我想把多条SQL语句做成同一事务,例如如
insert into t where ....
delete from t where ....
我写成这样:
begin tran
insert into t where ....
delete from t where ....
commit tran在第一条insert语句因插入时发生主键冲突而报错,接下来的delete语句还是把记录删除了,为什么呢?同一个事务里,应该不会被删除的啊.
insert into t where ....
delete from t where ....
我写成这样:
begin tran
insert into t where ....
delete from t where ....
commit tran在第一条insert语句因插入时发生主键冲突而报错,接下来的delete语句还是把记录删除了,为什么呢?同一个事务里,应该不会被删除的啊.
rollback tran
BEGIN TRANSACTION
insert into t where ....
if @@error <> 0
begin
raiserror('插入时发生错误,本次插入失败!',16,1)
rollback /*若插入时发生错误则回滚整个事务*/
return /*若发生错误则中止执行而返回*/
end
delete from t where ....
if @@error <> 0
begin
raiserror('删除时发生错误,本次删除失败!',16,1)
rollback /*若删除时发生错误则回滚整个事务*/
return /*若发生错误则中止执行而返回*/
end
----提交事务
commit transaction
insert into t where ....
delete from t where ....
commit tran
begin tran
insert into t where ....
if @@error <> 0
begin
rollback tran
return
end
delete from t where ....
commit tran
set xact_abort on在sybase里有类似的命令吗,因为我现在用的是SYBASE数据库.