程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> 其他數據庫知識 >> 更多數據庫知識 >> 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