START TRANSACTION;
INSERT INTO A VALUES(NULL,'2');
INSERT INTO A VALUES(NULL,'1');
COMMIT;
第二个字段是UNIQUE的,'1'已经存在,'2'没有存在
执行后,'2'还是被插入了,事务不是所有语句成功才执行吗?
INSERT INTO A VALUES(NULL,'2');
INSERT INTO A VALUES(NULL,'1');
COMMIT;
第二个字段是UNIQUE的,'1'已经存在,'2'没有存在
执行后,'2'还是被插入了,事务不是所有语句成功才执行吗?
而且你最后已经COMMIT 表示就算出错也会执行beigin tran //开始
declare @error1 int //设置变量接收异常
if @error<>0 //如果有异常 回滚
rollback tran
else
commit tran //否则提交
go
begin tran;
insert into dbo.t select 'aewdf','2','asrf'
INSERT INTO A VALUES(NULL,'2');--表不存在
INSERT INTO A VALUES(NULL,'1');--表不存在if @@error<>0 --如果有异常 回滚
begin
select @@error
rollback tran
end
else
begin
commit tran --否则提交
end
/*
(1 行受影响)
消息 208,级别 16,状态 1,第 4 行
对象名 'A' 无效。
*/
select * from t
/*
--无任何数据
*/
--一旦存在错误,只提交成功的
SET XACT_ABORT off
begin tran;
insert into dbo.t select 'aewdf','2','asrf'
INSERT INTO A VALUES(NULL,'2');--表不存在
INSERT INTO A VALUES(NULL,'1');--表不存在if @@error<>0 --如果有异常 回滚
begin
select @@error
rollback tran
end
else
begin
commit tran --否则提交
end
go
/*
(1 行受影响)
消息 208,级别 16,状态 1,第 4 行
对象名 'A' 无效。
*/
select * from t
/*
第一行正常提交
id name 2123 sefa
----------- ---------------------------------------- -------------------------------------------------- --------------------------------------------------
19 aewdf 2 asrf
20 aewdf 2 asrf
*/
http://tianjianbin203.blog.163.com/blog/static/865684592010020105244896/