SQL SERVER 事务的使用(tran)
--创建一个表,设置一个字段不为空 create table Student(Name nvarchar(20) not null)
Num One:--当操作(增删改)是由null引发的错误时,事务会跳过错误继续执行正常的语句。
begin tran insert into Student(Name) values (null) insert into Student(Name) values ('bob') insert into Student(Name) values (null) commit tran
--【方法 一】:当操作(增删改)是由null引发的错误时,@@ERROR<>0 事务遇到错误就不会继续执行,全部不会写入
begin tran insert into Student(Name) values (null) insert into Student(Name) values ('666') insert into Student(Name) values (null) if @@ERROR>0 -- begin rollback tran return end commit tran
--【方法 二】:xact_abort on/off on:开启,事务一旦出问题,全部回滚 off:关闭,不检查事务是否发生错误。
abort:中止 xact(没查出来啥意思)
set xact_abort on --如果是off,那么符合条件的会写入,异常的不会写入 begin tran insert into Student(Name) values (null) insert into Student(Name) values ('xact_abort') insert into Student(Name) values (null) commit
--【方法三】:try catch
begin tran begin try insert into Student(Name) values ('try catch') insert into Student(Name) values ('try catch') insert into Student(Name) values (null) end try begin catch if @@TRANCOUNT>0 rollback tran end catch if @@TRANCOUNT>0 commit tran --在SqlServer里,嵌套事务的层次是由@@TranCount全局变量反映出来的。 --每一次Begin Transaction都会引起@@TranCount加1。 --每一次Commit Transaction都会使@@TranCount减1, --RollBack Transaction会回滚所有的嵌套事务包括已经提交的事务和未提交的事务, --而使@@TranCount置0 --so 如果@@TRANCOUNT>0的话 那就是有begin tran有提交 --如果有异常,就进到catch里,然后@@TRANCOUNT又是大于0的,就是会全部回滚 --没有catch中,那 就是全部提交
--【方法四】:save transaction
begin transaction tran_A -- 最好是为事务定义一个名字。 insert into Student(Name) values ('save transaction4') insert into Student(Name) values (null) save transaction save_tran; --定义一个事务的保存点、当要回滚事务时,可以回滚到这里。 rollback transaction save_tran;--回滚事务到保存点 commit transaction tran_A;-- 提交事务。
网上的一个老哥的Code
begin transaction tran_A -- 最好是为事务定义一个名字。 insert into Nums(X) values(9); save transaction save_tran; --定义一个事务的保存点、当要回滚事务时,可以回滚到这里。 insert into Nums(X) values(4),(3),(2),(1); rollback transaction save_tran;--回滚事务到保存点 commit transaction tran_A;-- 提交事务。
CREATE PROCEDURE [dbo].[CreateRPNo] AS Declare @NO int Declare @ReturnNo nvarchar(20) BEGIN TRAN BEGIN select @NO = SERIAL_NO from RPO_SERIAL_NO select @NO = @NO + 1 update RPO_SERIAL_NO set SERIAL_NO = @NO select @ReturnNo = convert(nvarchar(20),@NO) END IF @@ERROR = 0 BEGIN select @ReturnNo as NO COMMIT TRAN END ELSE BEGIN ROLLBACK TRAN END GO
CREATE PROCEDURE [dbo].[CreateNumber] AS BEGIN Declare @NO int Declare @sNO nvarchar(20) Declare @date datetime
BEGIN TRAN
BEGIN Set @date = getdate() if exists(select * From Ho where YEAR(FlowNoDate)=YEAR(@date) AND MONTH(FlowNoDate)=MONTH(@date) AND DAY(FlowNoDate)=DAY(@date)) begin select @NO = MaxFlowNo From Ho where YEAR(FlowNoDate)=YEAR(@date) AND MONTH(FlowNoDate)=MONTH(@date) AND DAY(FlowNoDate)=DAY(@date) select @NO = @NO + 1 update HQeHubFlowNo set MaxFlowNo = @NO, FlowNoDate=@date where YEAR(FlowNoDate)=YEAR(@date) AND MONTH(FlowNoDate)=MONTH(@date) AND DAY(FlowNoDate)=DAY(@date) end else begin insert into Ho(MaxFlowNo,FlowNoDate) values(1,@date) select @NO = 1 end Set @sNo = convert(nvarchar(20),@NO) Set @sNo = '000000000' + @sNo Set @sNo = RIGHT(@sNo,9) END IF @@ERROR = 0 BEGIN select ('ZKB' + replace(convert(varchar,@date,111),'/','') + @sNo) as NO COMMIT TRAN END ELSE BEGIN ROLLBACK TRAN END END GO
CREATE PROCEDURE [dbo].[GetOMS_ID] ( @ORDER_TYPE_CODE nvarchar(20) ) AS Declare @SESSIONID nvarchar(20) Declare @i int Declare @ReturnNo nvarchar(30) Declare @strMonth nvarchar(20) BEGIN declare @Now datetime = getdate(); declare @Year int = year(@Now); declare @Month int = month(@Now); declare @Day int = day(@Now); BEGIN TRAN BEGIN if exists(select * from OMS_ID WHERE DD = @Day) begin SELECT @SESSIONID=ID FROM OMS_ID WHERE ORDER_TYPE_CODE = @ORDER_TYPE_CODE AND YY = @Year ; set @SESSIONID=@SESSIONID+1; UPDATE OMS_ID SET ID=@SESSIONID, UpdateDateTime=getdate()WHERE YY = @Year AND MM = @Month and DD = @Day; end else begin insert into OMS_ID(YY,MM,DD,ORDER_TYPE_CODE,ID,UpdateDateTime)values(@Year,@Month,@Day,@ORDER_TYPE_CODE,1,getdate()); set @SESSIONID=1; end declare @syy nvarchar(2) = right(convert(nvarchar(10),@Year),2); declare @smm nvarchar(2) = right('0' + convert(nvarchar(10),@Month),2); declare @sdd nvarchar(2) = right('0' + convert(nvarchar(10),@Day),2); declare @sid nvarchar(4) = right('0000' + convert(nvarchar(20),@SESSIONID),4); SET @ReturnNo = @ORDER_TYPE_CODE + @syy + @smm + @sdd + @sid; END IF @@ERROR = 0 BEGIN select @ReturnNo as NO COMMIT TRAN END ELSE BEGIN ROLLBACK TRAN END END GO