22,209
社区成员
发帖
与我相关
我的任务
分享
--=============================
create table testtran
(
id int identity(1,1),
lasttime datetime null
);
go
alter proc test1
as
begin
SET XACT_ABORT off
begin tran
insert into testtran(lasttime) values(GETDATE())
select lasttime=GETDATE() into #tmp
insert into testtran(errorname) select * from #tmp
end
go
alter proc test2
as
begin
SET XACT_ABORT on
begin tran
insert into testtran(lasttime) values(GETDATE())
select lasttime=GETDATE() into #tmp
insert into testtran(errorname) select * from #tmp
end
go
truncate table testtran
--1.====直接以脚本执行
begin tran
insert into testtran(lasttime) values(GETDATE())
select lasttime=GETDATE() into #tmp
insert into testtran(errorname) select * from #tmp
--====消息:
--(1 行受影响)
--(1 行受影响)
--消息 207,级别 16,状态 1,第 4 行
--列名 'errorname' 无效。
select * from testtran--一条数据
select @@TRANCOUNT--1, 事务未能提交或撤销
drop table #tmp
rollback tran--这里撤销一下事务,继续测下面的, 撤销后刚刚那一条数据也没了
--2.=====执行test1
exec test1
--====消息:
--(1 行受影响)
--(1 行受影响)
--消息 207,级别 16,状态 1,过程 test1,第 8 行
--列名 'errorname' 无效。
--消息 266,级别 16,状态 2,过程 test1,第 8 行
--EXECUTE 后的事务计数指示 BEGIN 和 COMMIT 语句的数目不匹配。上一计数 = 0,当前计数 = 1。
select * from testtran--0行数据
select @@TRANCOUNT--1, 事务未能提交或撤销
--3.====执行test2
exec test2
--====消息:
--(1 行受影响)
--(1 行受影响)
--消息 207,级别 16,状态 1,过程 test2,第 8 行
--列名 'errorname' 无效。
select * from testtran--0行数据
select @@TRANCOUNT--0 没有待提交的事务
drop table testtran