sql server 2005事务处理
时间:2011-11-23
来源:互联网
以下的SQL Server 2005语句:
Insert into score_total(oldernumber,score_sort,score_date) values('100200','入院','20110908')
declare @total_id int
select @total_id=@@identity
insert into score_detail (score_id,scoreProject_sort_id,reference_id,acture_score) values(@total_id,1,1,0)
insert into score_detail (score_id,scoreProject_sort_id,reference_id,acture_score) values(@total_id,1,1,2)
如果我在acture_score 字段中设置约束为:acture_score<2
执行第三条语句时:违反上面的约束,那我该如何让这三条语句通过事务.回滚回去,这三条记录要么全部执行,要么全都不执行.
作者: liqiyou 发布时间: 2011-11-23
declare @total_id int BEGIN TRANSACTION Insert into score_total(oldernumber,score_sort,score_date) values('100200','入院','20110908') select @total_id=@@identity insert into score_detail (score_id,scoreProject_sort_id,reference_id,acture_score) values(@total_id,1,1,0) insert into score_detail (score_id,scoreProject_sort_id,reference_id,acture_score) values(@total_id,1,1,2) commit
作者: qianjin036a 发布时间: 2011-11-23
SQL code
declare @total_id int BEGIN TRANSACTION Insert into score_total(oldernumber,score_sort,score_date) values('100200','入院','20110908') select @total_id=@@identity insert into score_detail (score_id,scoreProject_sort_id,reference_id,acture_score) values(@total_id,1,1,0) insert into score_detail (score_id,scoreProject_sort_id,reference_id,acture_score) values(@total_id,1,1,2) IF (@@error <> 0) ROLLBACK TRANSACTION ELSE COMMIT TRANSACTION
作者: qianjin036a 发布时间: 2011-11-23
declare @total_id int BEGIN TRANSACTION set XACT_ABORT ON Insert into score_total(oldernumber,score_sort,score_date) values('100200','入院','20110908') select @total_id=@@identity insert into score_detail (score_id,scoreProject_sort_id,reference_id,acture_score) values(@total_id,1,1,0) insert into score_detail (score_id,scoreProject_sort_id,reference_id,acture_score) values(@total_id,1,1,2) commit
作者: fredrickhu 发布时间: 2011-11-23
/*********************************************************************************** 整理人:中国风(Roy) 在这里举3种在SQL2005的事务写法 ************************************************************************************/ use test go create table t (ID int) go begin try begin tran insert T values(1) insert T values('a') commit tran end try begin catch select ERROR_MESSAGE() AS ErrorMessage; rollback tran end catch go begin tran begin try insert T values(1) insert T values('a') end try begin catch select ERROR_MESSAGE() AS ErrorMessage; if @@trancount>0 rollback tran end catch if @@trancount>0 commit tran go set xact_abort on ; begin try begin tran insert T values(1) insert T values('a') commit tran end try begin catch select ERROR_MESSAGE() AS ErrorMessage; if (xact_state())=-1 rollback tran if (xact_state())=1 commit tran end catch go select * from T
作者: roy_88 发布时间: 2011-11-23
SQL code
begin try begin tran Insert into score_total(oldernumber,score_sort,score_date) values('100200','入院','20110908') declare @total_id int select @total_id=@@identity insert into score_detail (score_id,scoreProject_sort_id,reference_id,acture_score) values(@total_id,1,1,0) insert into score_detail (score_id,scoreProject_sort_id,reference_id,acture_score) values(@total_id,1,1,2) commit tran end try begin catch rollback tran end catch
作者: ap0405140 发布时间: 2011-11-23
declare @total_id int set XACT_ABORT ON Insert into score_total(oldernumber,score_sort,score_date) values('100200','入院','20110908') select @total_id=@@identity insert into score_detail (score_id,scoreProject_sort_id,reference_id,acture_score) values(@total_id,1,1,0) insert into score_detail (score_id,scoreProject_sort_id,reference_id,acture_score) values(@total_id,1,1,2)
作者: public0011 发布时间: 2011-11-23
BEGIN TRY BEGIN TRAN declare @total_id int set XACT_ABORT ON Insert into score_total(oldernumber,score_sort,score_date) values('100200','入院','20110908') select @total_id=@@identity insert into score_detail (score_id,scoreProject_sort_id,reference_id,acture_score) values(@total_id,1,1,0) insert into score_detail (score_id,scoreProject_sort_id,reference_id,acture_score) values(@total_id,1,1,2) COMMIT TRAN END TRY BEGIN CATCH IF @@error<>0 ROLLBACK TRAN SELECT ERROR_LINE(),ERROR_MESSAGE(),ERROR_STATE() END CATCH
作者: public0011 发布时间: 2011-11-23
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28