zoukankan      html  css  js  c++  java
  • SQL Server 事务处理 回滚事务

     创建表:

    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    CREATE TABLE [dbo].[t1](
        
    [Id] [int] NOT NULL,
        
    [c1] [nvarchar](50NULL,
        
    [c2] [datetime] NULL,
     
    CONSTRAINT [PK_Table1] PRIMARY KEY CLUSTERED 
    (
        
    [Id] ASC
    )
    WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ONON [PRIMARY]
    ON [PRIMARY]

     解决方案(一)


    declare   @iErrorCount   int 
    set @iErrorCount = 0
    begin tran Tran_2008_10_07

    insert into t1(Id, c1) values(1,'1')
    set @iErrorCount=@iErrorCount+@@error

    insert into t1(Id, c1) values(2,'2')
    set @iErrorCount=@iErrorCount+@@error

    insert into t1(Id, c1) values('xxxx3','3')
    set @iErrorCount=@iErrorCount+@@error

    insert into t1(Id, c1) values(4,'4')
    set @iErrorCount=@iErrorCount+@@error

    insert into t1(Id, c1) values(5,'5')
    set @iErrorCount=@iErrorCount+@@error

    if @iErrorCount=0 
      
    begin   
        
    COMMIT TRAN Tran_2008_10_07
      
    end 
    else   
      
    begin   
        
    ROLLBACK TRAN Tran_2008_10_07
      
    end 

     解决方案(二)

    begin try
        
    begin tran Tran_2008_10_07

            
    insert into t1(Id, c1) values(1,'1')

            
    insert into t1(Id, c1) values(2,'2')

            
    insert into t1(Id, c1) values('xxxx3','3')

            
    insert into t1(Id, c1) values(4,'4')

            
    insert into t1(Id, c1) values(5,'5')

        
    COMMIT TRAN Tran_2008_10_07
    end try 
    begin catch 
        
    raiserror 50005N'出错了' 
        
    ROLLBACK TRAN Tran_2008_10_07
    end catch 
  • 相关阅读:
    ureport2 数据源配置
    ureport2 + spring boot 搭建
    alibaba
    Maven Helper
    在Idea中连接数据库并生成实体类
    Intellij jrebel 热部署 安装
    IDEA使用说明
    JPA 常用注解 (hibernate)
    vue + canvas 图片加水印
    vue——批量下载图片
  • 原文地址:https://www.cnblogs.com/emanlee/p/1305424.html
Copyright © 2011-2022 走看看