begin  tran DELETE from tabel where ID = 11111(这个ID是不存在的)
 if  @@error<>0
 begin
  rollback  tran
  return  
 end    
 DELETE from tabel where ID = 11(这个ID存在)
 if  @@error<>0
 begin
  rollback  tran
  return          
 end commit  tran
 
这样写没用,只要其中一个ID存在就可以删掉了。
我想实现要么两条都删除,要么两条都不删除,怎么写?我知道@@error是错误编号的意思,但是
DELETE from tabel where ID = 11111 
只是这个ID不存在,并不产生错误,是这样吗?

解决方案 »

  1.   

    --这样试一下!
    SET XACT_ABORT ON
    DELETE from tabel where ID = 11111
    DELETE from tabel where ID = 11
    SET XACT_ABORT OFF
      

  2.   

    DELETE from tabel where ID = 11111 
    只是这个ID不存在,并不产生错误,是这样吗?是的(所影响的行数为 0 行)@@error=0
      

  3.   

    begin  tran
     if exists(select 1 from tabel where ID=11111)
    begin
     DELETE from tabel where ID = 11111(这个ID是不存在的)
     if  @@error<>0
     begin
      rollback  tran
      return  
     end    
    end
    else
    --回滚代码
    if exists(select 1 from tabel where ID=11111)
    begin
     DELETE from tabel where ID = 11(这个ID存在)
     if  @@error<>0
     begin
      rollback  tran
      return          
     end 
    end
    else
    --回滚代码
    commit  tran
      

  4.   

    to: liangpei2008(逍遥叹) 
    不行to :xyxfly(小虾米 -_- 何去何从)
    if exists()
    真的要这么麻烦吗?
      

  5.   

    DELETE from tabel where ID = 11111 
    只是这个ID不存在,并不产生错误,是这样吗?是的(所影响的行数为 0 行)@@error=0因为这个原因,没有错误你也要回滚,所以需要判断是不是存在
    对了,可以试试@@rowcount
      

  6.   

    begin  tran DELETE from tabel where ID = 11111(这个ID是不存在的)
     if  @@error<>0 and @@rowcount<>0
     begin
      rollback  tran
      return  
     end    
     DELETE from tabel where ID = 11(这个ID存在)
     if  @@error<>0 and @@rowcount<>0 begin
      rollback  tran
      return          
     end commit  tran
      

  7.   

    可用    @@rowcount   最近一次执行T-SQL语句时返回的行数。我也是刚刚找到的。