zoukankan      html  css  js  c++  java
  • SQLServer存储过程中的简单事务处理

    一. 添加记录

    需要事务支持,如果上下文环境中不存在事务,新启动一个事务,否则利用已存在的事务.

    -- =============================================
    -- Author:         <Author,,Name>
    -- Create date: <Create Date,,>
    -- Description:    <Description,,>
    -- =============================================
    CREATE PROCEDURE [dbo].[AddJob]
         @jobName nvarchar(50),
         @added bit output
    AS
    BEGIN
         -- SET NOCOUNT ON added to prevent extra result sets from
         -- interfering with SELECT statements.
         SET NOCOUNT ON;

        DECLARE @errorCode int
        SET @errorCode = 0

        --Transaction identity. 1:Created a new transaction. 0:Use old transaction.
        DECLARE @tranStarted bit
        SET @tranStarted = 0

        --Create a new transaction if there is not a transaction.
        IF( @@TRANCOUNT = 0 )
        BEGIN
            BEGIN TRANSACTION
            SET @tranStarted = 1
        END
        ELSE
            SET @tranStarted = 0

        --Adding operation.
        INSERT INTO dbo.Job(JobName)
             VALUES (@jobName)

        IF ( @@ERROR <> 0 )
        BEGIN
            SET @errorCode = -1
            GOTO Cleanup
        END

        IF ( @tranStarted = 1 )
        BEGIN
            SET @tranStarted = 0
            COMMIT TRANSACTION
        END
       
        SET @added = 1
        RETURN(0)

    --Error handler.
    Cleanup:
        IF( @tranStarted = 1 )
        BEGIN
            SET @tranStarted = 0
           ROLLBACK TRANSACTION
        END

        SET @added = 0
        RETURN @errorCode  
    END
    二. 删除记录(同上)
       需要事务支持,如果上下文环境中不存在事务,新启动一个事务,否则利用已存在的事务.
    三. 修改记录(同上)
       需要事务支持,如果上下文环境中不存在事务,新启动一个事务,否则利用已存在的事务.
    四. 查询记录
       不需要事务支持.
    -- =============================================
    -- Author:         <Author,,Name>
    -- Create date: <Create Date,,>
    -- Description:    <Description,,>
    -- =============================================
    CREATE PROCEDURE [dbo].[GetAllJob]
    AS
    BEGIN
         -- SET NOCOUNT ON added to prevent extra result sets from
         -- interfering with SELECT statements.
         SET NOCOUNT ON;
       
        SELECT JobId, JobName
        FROM Job
    END


    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/fishinthewind/archive/2007/05/15/1610771.aspx

  • 相关阅读:
    什么是MongoDb
    Python之人工智能:PyAudio 实现录音 自动化交互实现问答
    Python人工智能之初识接口
    cordova(安卓)(腾讯信鸽注册绑定与反绑定) 插件开发
    sencha touch 在安卓中横屏、竖屏切换 应用崩溃问题
    Sencha Touch 实战开发培训 电子书 基础篇
    wps 批量调整图片大小 宏
    使用 crosswalk-cordova 打包sencha touch 项目,再也不用担心安卓兼容问题!
    Sencha Cmd 5.0.1.231 是坑爹货
    sencha touch api 使用指南
  • 原文地址:https://www.cnblogs.com/xinzhuangzi/p/4100656.html
Copyright © 2011-2022 走看看