sqlserver 在存储过程中的游标内使用两个update出现错误“子查询返回的值不止一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。” 
游标为两次嵌套
在最内部游标循环中,两次使用update就出现上述错误,第一个没有问题,第二个有问题
单独执行第二个也没有问题
第一个update语句:update tb set col1 = @val1 where col2 = @val2
第二个update语句:update tb2 set col1 = @val1 where col2 = @val2 and col3 = N'Name'

解决方案 »

  1.   

    全部源码:
    ALTER PROCEDURE [dbo].[UpdateBoatman]
    @船员 nvarchar(max),
    @无部门船员 nvarchar(max)
    AS
    BEGIN
    declare @temptb table(艇名 nvarchar(20),顺序 int)
    insert into @temptb select 艇号,ROW_NUMBER() over(order by 艇号) from dbo.BoatStatus order by 艇号

    declare @BoatNum smallint
    set @BoatNum = 0
    declare @tempBoatman nvarchar(255)
    declare GetBoatmen cursor
    for select * from dbo.SplitStrs(@船员,N'|')
    open GetBoatmen
    fetch next from GetBoatmen into @tempBoatman
    while(@@fetch_status=0)
    begin
    set @BoatNum = @BoatNum + 1
    declare @PatrolName nvarchar(20)
    set @PatrolName = (select 艇名 from @temptb where 顺序 = @BoatNum)
    declare @tempDepart nvarchar(20)
    set @tempDepart = (select 待命点 from dbo.BoatStatus where 艇号 = @PatrolName)

    declare @Boatman nvarchar(20)
    set @Boatman = N''
    declare GetBoatName cursor
    for select * from dbo.SplitStrs(@tempBoatman,N',')
    open GetBoatName
    fetch next from GetBoatName into @Boatman
    while(@@FETCH_STATUS=0)
    begin
    if exists(select 1 from dbo.Boatman where 姓名 = @Boatman) begin
    update dbo.Boatman set 跟艇 = @PatrolName where 姓名 = @Boatman

    --在用户表中更新对应船员的部门,船员部门根据艇所在的部门获得
    --if exists(select 1 from [dbo].[User] where 姓名 = @Boatman and 职务 = N'船员') begin
    -- update [dbo].[User] set 部门 = @tempDepart where 姓名 = @Boatman and 职务 = N'船员' --end
    end

    fetch next from GetBoatName into @Boatman
    end
    close GetBoatName
    deallocate GetBoatName

    fetch next from GetBoatmen into @tempBoatman
    end
    close GetBoatmen
    deallocate GetBoatmen

    declare @tempNoDB nvarchar(20)
    declare GetNoDB cursor
    for select * from dbo.SplitStrs(@无部门船员,N',')
    open GetNoDB
    fetch next from GetNoDB into @tempNoDB
    while(@@FETCH_STATUS=0)
    begin
    if exists(select 1 from dbo.Boatman where 姓名 = @tempNoDB) begin
    update dbo.Boatman set 跟艇 = N'' where 姓名 = @tempNoDB
    end
    fetch next from GetNoDB into @tempNoDB
    end
    close GetNoDB
    deallocate GetNoDB
    END红字的为执行出错的地方,谢谢各位高手了
      

  2.   

    我用sqlserver2008,在里面写的做也,怎么移到新服务器上,谢谢!
      

  3.   

    删除触发器了,用其他方法解决了
    现在遇到个新问题,我用sqlserver2008,在代理写的作业,怎么移到新服务器上,谢谢!,分都压到这个问题上了