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不存在,并不产生错误,是这样吗?
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不存在,并不产生错误,是这样吗?
SET XACT_ABORT ON
DELETE from tabel where ID = 11111
DELETE from tabel where ID = 11
SET XACT_ABORT OFF
只是这个ID不存在,并不产生错误,是这样吗?是的(所影响的行数为 0 行)@@error=0
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
不行to :xyxfly(小虾米 -_- 何去何从)
if exists()
真的要这么麻烦吗?
只是这个ID不存在,并不产生错误,是这样吗?是的(所影响的行数为 0 行)@@error=0因为这个原因,没有错误你也要回滚,所以需要判断是不是存在
对了,可以试试@@rowcount
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