Sqlserver 存储过程中结合事务的代码

复制代码 代码如下:

--方式一

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[USP_ProcedureWithTransaction_Demo]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)

drop procedure [dbo].[USP_ProcedureWithTransaction_Demo]

GO

-- =============================================

-- Author: <ChengXiaoming>

-- Create date: <2010-06-11>

-- Description: <Demo:存储过程中使用事务>

-- =============================================

Create PROCEDURE [dbo].[USP_ProcedureWithTransaction_Demo]

As

Begin

SET XACT_ABORT ON

Begin Transaction

Insert Into Lock(LockTypeID) Values('A')--此语句将出错,LockTypeID为Int类型

Update Lock Set LockTypeID = 2 Where LockID = 32

Commit Transaction

SET XACT_ABORT OFF

End

GO

--方式二

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[USP_ProcedureWithTransaction_Demo]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)

drop procedure [dbo].[USP_ProcedureWithTransaction_Demo]

GO

-- =============================================

-- Author: <ChengXiaoming>

-- Create date: <2010-06-11>

-- Description: <Demo:存储过程中使用事务>

-- =============================================

Create PROCEDURE [dbo].[USP_ProcedureWithTransaction_Demo]

As

Begin

Begin Transaction

Insert Into Lock(LockTypeID) Values('A')--此语句将出错,LockTypeID为Int类型

Update Lock Set LockTypeID = 1 Where LockID = 32

Commit Transaction

If(@@ERROR <> 0)

Rollback Transaction

End

GO

--方式三

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[USP_ProcedureWithTransaction_Demo]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)

drop procedure [dbo].[USP_ProcedureWithTransaction_Demo]

GO

-- =============================================

-- Author: <ChengXiaoming>

-- Create date: <2010-06-11>

-- Description: <Demo:存储过程中使用事务>

-- =============================================

Create PROCEDURE [dbo].[USP_ProcedureWithTransaction_Demo]

As

Begin

Begin Try

Begin Transaction

Update Lock Set LockTypeID = 1 Where LockID = 32--此语句将出错,LockTypeID为Int类型

Insert Into Lock(LockTypeID) Values('A')

Commit Transaction

End Try

Begin Catch

Rollback Transaction

End Catch

End

GO

Exec [USP_ProcedureWithTransaction_Demo]

时间: 2024-07-31 10:25:46

Sqlserver 存储过程中结合事务的代码的相关文章

Sqlserver 存储过程中结合事务的代码_MsSql

复制代码 代码如下: --方式一 if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[USP_ProcedureWithTransaction_Demo]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[USP_ProcedureWithTransaction_Demo] GO -- ===================

SQLServer存储过程中事务的使用方法_MsSql

本文为大家分享了SQLServer存储过程中事务的使用方法,具体代码如下 create proc usp_Stock @GoodsId int, @Number int, @StockPrice money, @SupplierId int, @EmpId int, @StockUnit varchar(50), @StockDate datetime, @TotalMoney money , @ActMoney money , @baseId int, @Description nvarcha

SQLServer存储过程中事务的使用方法

本文为大家分享了SQLServer存储过程中事务的使用方法,具体代码如下 create proc usp_Stock @GoodsId int, @Number int, @StockPrice money, @SupplierId int, @EmpId int, @StockUnit varchar(50), @StockDate datetime, @TotalMoney money , @ActMoney money , @baseId int, @Description nvarcha

SQL中存储过程中使用事务,并且加入异常处理机制.

--存储过程中使用事务,并且加入异常处理机制. -- ============================================= CREATE PROCEDURE [dbo].[UP_Orders_Import] AS BEGIN BEGIN TRAN --开启事务 BEGIN TRY SELECT 1/0 COMMIT TRAN --提交事务 END TRY BEGIN CATCH DECLARE @ErrorMessage NVARCHAR(4000) , @ErrorNum

MySQL存储过程中的事务管理实例说明

mysql存储过程中事务SQL代码  delimiter $$  use test$$  create procedure t_insert_table()  begin      /** 标记是否出错 */      declare t_error int default 0;      /** 如果出现sql异常,则将t_error设置为1后继续执行后面的操作 */      declare continue handler for sqlexception set t_error=1; -

sqlserver 存储过程中If Else的用法实例

为大家介绍sql server存储过程中if esle的用法,供大家学习参考.数据库中有两张表,A表主键为自动增长的并且是B表的外键且允许为空   现在要通过编程向B表中插入数据,可是在程序中是不允许给Int类型赋空值的如果不赋值就默认为0. 为了解决这个问题,用到了存储过程的If Else,下面是完整的存储过程. 代码示例: 复制代码 代码如下: create PROCEDURE [dbo].[P_Form_Control_Info_Add]     @TypeName varchar(20)

sqlserver 存储过程中的top+变量使用分析(downmoon)_mssql2005

存储过程中的TOP后跟一个变量会如何? 复制代码 代码如下: Create proc getWorkPlan2 (@intCounter int ,@lngUserID int) as select Top 5 lngWorkID,strWorkName,strExecHumanName,strBeginDate from worklist where lngExecHumanID= @lngUserID order by lngWorkID desc 现在想将这里的Top 5 改为变量· To

在Mysql存储过程中使用事务实例_Mysql

复制代码 代码如下: CREATE DEFINER=`root`@`localhost` PROCEDURE `createBusiness`(parameter1 int)BEGIN    #Routine body goes here...    DECLARE flag int DEFAULT parameter1;#声明变量flag,将参数值赋给该变量    DECLARE uuidStr VARCHAR(32);#声明一个长度为32位的字符串    DECLARE currentTim

sqlserver 存储过程中If Else的用法实例_MsSql

现在要通过编程向B表中插入数据,可是在程序中是不允许给Int类型赋空值的如果不赋值就默认为0.为了解决这个问题,用到了存储过程的If Else,下面是完整的存储过程. 代码示例: 复制代码 代码如下: create PROCEDURE [dbo].[P_Form_Control_Info_Add]    @TypeName varchar(20),    @Description varchar(50),    @CtlColSpan int,    @Sort int,    @SourceI