你这种使用游标肯定错了。因为@@fetch_status不一致。你给出两的表结构,和两个表的测试数据,还有你要的结果数据。
然后大家给你写。

解决方案 »

  1.   

    好像没必要用游标吧,应该可以一句搞定的。
    update order
    set sid=2040
    where not exists(select 1 from orderdetail b where order.id=b.id and b.flag<>0)
      

  2.   

    谢谢wzh1215(四脚蛇) ,果然是一句话搞定,不过我还是想知道用游标怎么搞定
      

  3.   

    try below:
    declare @PID int --ProductID
    declare @OID int --OrderID
    declare @flag intdeclare @flagcount int
    declare @flag_total intselect @flagcount=0declare orderdetail_cursor cursor for
    select pid,flag from OrderDetail
    where oid = @OIDopen orderdetail_cursorfetch next from orderdetail_cursor into @PID,@flag
    while @@fetch_status = 0
    begin
    if @flag = 0
    begin
    select @flagcount=@flagcount+1
    fetch next from orderdetail_cursor into @PID,@flag
    end else 
    if @flag > 0
    begin
    select @flagcount=-1
    break
    end

    endselect @flag_total=count(flag) from OrderDetail
    if @flagcount=@flag_total
    begin
    update [order] set sid = 2040 where oid = @OID
    end
    else
    begin
    update [order] set sid = -1 where oid = @OID
    endclose order_cursor
    deallocate order_cursor