程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> 其他數據庫知識 >> MSSQL >> Sqlserver 存儲進程中聯合事務的代碼

Sqlserver 存儲進程中聯合事務的代碼

編輯:MSSQL

Sqlserver 存儲進程中聯合事務的代碼。本站提示廣大學習愛好者:(Sqlserver 存儲進程中聯合事務的代碼)文章只能為提供參考,不一定能成為您想要的結果。以下是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]
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved