• SQL Server 中的SET XACT_ABORT各种用法及显示结果


     

    源地址:http://www.cnblogs.com/rob0121/articles/2320932.html 点击进入

    默认行为:默认为SET XACT_ABORT OFF,没有事务行为。

    SET XACT_ABORT ON

    SET XACT_ABORT ON分为两种:

      1、总体作为一个事务,整体提交或整体回滚,格式为:

    SET XACT_ABORT ON
    BEGIN TRAN
        --要执行的语句
    COMMIT TRAN
    GO
    View Code

    2、每个语句作为一个事务,事务在错误行终止,错误行回滚,错误行之前的不回滚,格式为:

    SET XACT_ABORT ON
    BEGIN
        --要执行的语句
    END
    GO
    View Code

    示例

    --创建测试表
    use MyDB
    CREATE TABLE student
    (    
            stuid int NOT NULL PRIMARY KEY,
            stuname varchar(50)
    )
    CREATE TABLE score 
    (
            stuid int NOT NULL REFERENCES student(stuid),
            score int
    )
    GO
    
    --插入测试数据
    INSERT INTO student VALUES (101,'zhangsan') 
    INSERT INTO student VALUES (102,'wangwu') 
    INSERT INTO student VALUES (103,'lishi') 
    INSERT INTO student VALUES (104,'maliu') 
    GO
    
    ---------------测试事务提交------------------
    use MyDB
    --只回滚错误行,语句还继续执行
    SET XACT_ABORT OFF
    BEGIN TRAN
        INSERT INTO score  VALUES (101,90)
        INSERT INTO score VALUES (102,78) 
        INSERT INTO score VALUES (107,76) /* Foreign Key Error */ 
        INSERT INTO score VALUES (103,81) 
        INSERT INTO score VALUES (104,65) 
    COMMIT TRAN
    GO
    /*
    stuid       score
    ----------- -----------
    101         90
    102         78
    103         81
    104         65
    
    (4 row(s) affected)
    */
    
    use MyDB
    --事务终止并全部回滚
    SET XACT_ABORT ON
    BEGIN TRAN
        INSERT INTO score  VALUES (101,90)
        INSERT INTO score VALUES (102,78) 
        INSERT INTO score VALUES (107,76) /* Foreign Key Error */ 
        INSERT INTO score VALUES (103,81) 
        INSERT INTO score VALUES (104,65) 
    COMMIT TRAN
    GO
    /*
    stuid       score
    ----------- -----------
    (0 row(s) affected)
    */
    
    use MyDB
    --事务在错误行终止,错误行回滚,错误行之前的不回滚
    SET XACT_ABORT ON
    BEGIN
    INSERT INTO score  VALUES (101,90)
        INSERT INTO score VALUES (102,78) 
        INSERT INTO score VALUES (107,76) /* Foreign Key Error */ 
        INSERT INTO score VALUES (103,81) 
        INSERT INTO score VALUES (104,65) 
    END
    GO
    /*
    stuid       score
    ----------- -----------
    101         90
    102         78
    (2 row(s) affected)
    */
    View Code
  • 相关阅读:
    input输入框与元素间有间隙
    显示3行,还要省略号(这个属性比较合适WebKit浏览器或移动端(绝大部分是WebKit内核的)浏览器)
    input file 修改按钮名称
    文本溢出处理
    移动WEB前端开发资源的一些素材
    带弹性的导航栏
    带重力的公告栏
    淘宝放大镜效果
    【规范】javascript 变量命名规则(转)
    常见的仿Flash图片轮播效果
  • 原文地址:https://www.cnblogs.com/qiailu/p/4350413.html
Copyright © 2020-2023  润新知