下面这个当触发器执行时就出现这个错误了:子查询返回的值多于一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。
语句已终止。 
alter trigger grigger_change_delete on [Game]
/*for update*/
after update
as
if not exists(select 1 from deleted d join inserted i on d.ID=i.ID and d.stateID=1 and i.stateID=2)
return
else
begin
if((select [stateID] from inserted)=2)
begin
/*当stateid值变成2时,将所有与之公司id对应的赔率表中的信息删除*/delete [AbPayoutRate] where [HashCode]=(select [abid] from [Game] where [stateID]=2)delete [AsePayoutRate] where [HashCode]=(select [aseid] from [Game] where [stateID]=2)delete [AstPayoutRate] where [HashCode]=(select [astid] from [Game] where [stateID]=2)delete [BinPayoutRate] where [HashCode]=(select [binID] from [Game] where [stateID]=2)delete [DaPayoutRate] where [HashCode]=(select [daID] from [Game] where [stateID]=2)delete [GaPayoutRate] where [HashCode]=(select [gaID] from [Game] where [stateID]=2)delete [HdPayoutRate] where [HashCode]=(select [hdID] from [Game] where [stateID]=2)delete [JpPayoutRate] where [HashCode] in (select [jpID] from [Game] where [stateID]=2)delete [SbPayoutRate] where [HashCode]=(select [sbID] from [Game] where [stateID]=2)delete [TaPayoutRate] where [HashCode]=(select [taID] from [Game] where [stateID]=2)
end
end

解决方案 »

  1.   

    select [abid] from [Game] where [stateID]=2
    这中地方选择出来的abid不止一个,你可以用in替换=号
      

  2.   


    --try:
    alter trigger grigger_change_delete on [Game] after update
    as
    if not exists(select 1 from deleted d join inserted i on d.ID=i.ID and d.stateID=1 and i.stateID=2)
    return
    else
    begin
    if exists(select 1 from inserted where [stateID]=2)
    begin
    /*当stateid值变成2时,将所有与之公司id对应的赔率表中的信息删除*/delete [AbPayoutRate] where [HashCode] in (select [abid] from [Game] where [stateID]=2)delete [AsePayoutRate] where [HashCode] in (select [aseid] from [Game] where [stateID]=2)delete [AstPayoutRate] where [HashCode] in (select [astid] from [Game] where [stateID]=2)delete [BinPayoutRate] where [HashCode] in (select [binID] from [Game] where [stateID]=2)delete [DaPayoutRate] where [HashCode] in (select [daID] from [Game] where [stateID]=2)delete [GaPayoutRate] where [HashCode] in (select [gaID] from [Game] where [stateID]=2)delete [HdPayoutRate] where [HashCode] in (select [hdID] from [Game] where [stateID]=2)delete [JpPayoutRate] where [HashCode] in (select [jpID] from [Game] where [stateID]=2)delete [SbPayoutRate] where [HashCode] in (select [sbID] from [Game] where [stateID]=2)delete [TaPayoutRate] where [HashCode] in (select [taID] from [Game] where [stateID]=2)
    end
    end
      

  3.   

    alter trigger grigger_change_delete on [Game]
    /*for update*/
    after update
    as
    if not exists(select 1 from deleted d join inserted i on d.ID=i.ID and d.stateID=1 and i.stateID=2)
    return
    else
    begin
    if((select [stateID] from inserted)=2)
    begin
    /*当stateid值变成2时,将所有与之公司id对应的赔率表中的信息删除*/delete [AbPayoutRate] where [HashCode] in (select [abid] from [Game] where [stateID]=2)delete [AsePayoutRate] where [HashCode] in (select [aseid] from [Game] where [stateID]=2)delete [AstPayoutRate] where [HashCode] in (select [astid] from [Game] where [stateID]=2)delete [BinPayoutRate] where [HashCode] in (select [binID] from [Game] where [stateID]=2)delete [DaPayoutRate] where [HashCode] in (select [daID] from [Game] where [stateID]=2)delete [GaPayoutRate] where [HashCode] in (select [gaID] from [Game] where [stateID]=2)delete [HdPayoutRate] where [HashCode] in (select [hdID] from [Game] where [stateID]=2)delete [JpPayoutRate] where [HashCode] in (select [jpID] from [Game] where [stateID]=2)delete [SbPayoutRate] where [HashCode] in (select [sbID] from [Game] where [stateID]=2)delete [TaPayoutRate] where [HashCode] in (select [taID] from [Game] where [stateID]=2)
    end
    end
      

  4.   

    应该是多笔更新造成的。
    提示信息已经说明的很清楚了 : 子查询返回的值多于一个。当子查询跟随在 =、!=、 <、 <=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。
    if((select [stateID] from inserted)=2)
    如果一次更新2笔或以上,这里将会返回两个或两个以上的stateid的值,这样就会出错,后面的语句也有这样的问题。
    至於怎么改,要看你的逻辑是怎样的了。
      

  5.   

    对的 sqlserver貌似没有oracle的for each row