SQL Server Stored Procedure Combined with the transaction code

Source: Internet
Author: User

Copy codeThe Code is as follows:
-- Method 1
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: transactions used in Stored Procedures>
-- ===================================================== ======
Create PROCEDURE [dbo]. [USP_ProcedureWithTransaction_Demo]
As
Begin
SET XACT_ABORT ON
Begin Transaction
Insert Into Lock (LockTypeID) Values ('A') -- this statement will have an error. LockTypeID is of the Int type.
Update Lock Set LockTypeID = 2 Where LockID = 32
Commit Transaction
SET XACT_ABORT OFF
End
GO

-- Method 2
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: transactions used in Stored Procedures>
-- ===================================================== ======
Create PROCEDURE [dbo]. [USP_ProcedureWithTransaction_Demo]
As
Begin
Begin Transaction
Insert Into Lock (LockTypeID) Values ('A') -- this statement will have an error. LockTypeID is of the Int type.
Update Lock Set LockTypeID = 1 Where LockID = 32
Commit Transaction
If (@ ERROR <> 0)
Rollback Transaction
End
GO

-- Method 3
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: transactions used in Stored Procedures>
-- ===================================================== ======
Create PROCEDURE [dbo]. [USP_ProcedureWithTransaction_Demo]
As
Begin
Begin Try
Begin Transaction
Update Lock Set LockTypeID = 1 Where LockID = 32 -- an error occurs in this statement. LockTypeID is of the Int type.
Insert Into Lock (LockTypeID) Values ('A ')
Commit Transaction
End Try
Begin Catch
Rollback Transaction
End Catch
End
GO

Exec [USP_ProcedureWithTransaction_Demo]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.