[SQL Server]事务异常处理不能及时回滚

wangchao1982 wangchao1982 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
事务异常处理不能及时回滚
问题重现的代码

begin transaction tKaiser
begin try
create table Test
(
[ID] int identity(1,1) primary key,
[Name] nvarchar(50) not null,
[Age] int check ([Age] between 1 and 200),
[Sex] int check ([Sex] in (0, 1))
)
--这里将会产生一个异常
select * from asdf
commit transaction tKaiser
print 'transaction is commit'
end try
begin catch
rollback transaction tKaiser;
end catch

go
--按理说,事务已经被回滚了,下面的查询是会报错的,可事实却不是如此
--如果再执行一下创见表的操作(我理解是,创建表操作,是有默认事务的,
--也就是说再新事务之前才会回滚上一次事务),则test表就会被删除了。我想应该是我对sql2005中的异常处理机制
--的理解有问题,请大人们帮忙解释一下。谢谢
insert into Test([Name], [Age], [Sex])
select 'kaiser', '20', '0'
select * from test
...全文
评论 10
收藏 已收藏
分享
返回顶部
wangchao1982
疑难问题 22,298 成员 121,727 内容