Sqlserver 存储过程中结合事务的代码
程序员文章站
2022-05-02 17:30:26
...
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:
-- Create date:
-- Description:
-- =============================================
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:
-- Create date:
-- Description:
-- =============================================
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:
-- Create date:
-- Description:
-- =============================================
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]
推荐阅读
-
sqlserver 存储过程中If Else的用法实例
-
SQLServer 2008中的代码安全(一) 存储过程加密与安全上下文
-
MYSQL存储过程中事务和DECLARE EXIT/CONTINUE HANDLER的使用
-
SQL Server存储过程中编写事务处理的方法小结
-
Sqlserver事务备份和还原的实例代码(必看)
-
SqlServer查询存储过程中包含指定的内容
-
C#语言和SQLServer数据库结合使用,通过数据库中的表实现存储数据
-
HEVC代码学习:帧间预测——MVP过程中MV的获取、传递及存储
-
SQLServer 2008中的代码安全(一) 存储过程加密与安全上下文
-
VB.Net程序设计:图片存储到SQLServer数据库里的界面代码。