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

    创建表:

    复制代码
    SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATETABLE[dbo].[t1](     [Id][int]NOTNULL,     [c1][nvarchar](50) NULL,     [c2][datetime]NULL, CONSTRAINT[PK_Table1]PRIMARYKEYCLUSTERED (     [Id]ASC )WITH (PAD_INDEX  =OFF, STATISTICS_NORECOMPUTE  =OFF, IGNORE_DUP_KEY =OFF, ALLOW_ROW_LOCKS  =ON, ALLOW_PAGE_LOCKS  =ON) ON[PRIMARY] ) ON[PRIMARY]
    复制代码

    解决方案(一)

    复制代码
    declare   @iErrorCount   intset@iErrorCount=0begintran Tran_2008_10_07
    insertinto t1(Id, c1) values(1,'1') set@iErrorCount=@iErrorCount+@@error
    insertinto t1(Id, c1) values(2,'2') set@iErrorCount=@iErrorCount+@@error
    insertinto t1(Id, c1) values('xxxx3','3') set@iErrorCount=@iErrorCount+@@error
    insertinto t1(Id, c1) values(4,'4') set@iErrorCount=@iErrorCount+@@error
    insertinto t1(Id, c1) values(5,'5') set@iErrorCount=@iErrorCount+@@error
    if@iErrorCount=0   begin       COMMITTRAN Tran_2008_10_07   endelse     begin       ROLLBACKTRAN Tran_2008_10_07   end
    复制代码

    解决方案(二)

    复制代码
    begin try     begintran Tran_2008_10_07
           
    insertinto t1(Id, c1) values(1,'1')
           
    insertinto t1(Id, c1) values(2,'2')
           
    insertinto t1(Id, c1) values('xxxx3','3')
           
    insertinto t1(Id, c1) values(4,'4')
           
    insertinto t1(Id, c1) values(5,'5')
       
    COMMITTRAN Tran_2008_10_07 end try begin catch     raiserror 50005N'出错了'     ROLLBACKTRAN Tran_2008_10_07 end catch
    复制代码

    http://www.cnblogs.com/emanlee/archive/2008/10/07/1305424.html

  • 相关阅读:
    Centos7-两台Centos机器间复制文件
    Centos7-卸载自带的jdk 安装jdk8
    java网络编程_IP地址
    多线程下单例模式的实现_ThreadLocal_ReentrantLock
    线程定时调度
    线程通信
    线程同步学习一
    java线程学习2
    java线程学习1
    工单系统的设计与实现(3)
  • 原文地址:https://www.cnblogs.com/cmblogs/p/3414439.html
Copyright © 2011-2022 走看看