Code that combines transactions in SQL Server stored procedures _mssql

Source: Internet
Author: User
Tags commit rollback
Copy Code code as follows:

--Way One
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: Using transactions 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 type int
Update Lock Set Locktypeid = 2 Where lockid = 32
Commit Transaction
SET Xact_abort off
End
Go

--Way Two
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: Using transactions 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 type int
Update Lock Set Locktypeid = 1 Where lockid = 32
Commit Transaction
If (@ @ERROR <> 0)
Rollback Transaction
End
Go

--Way Three
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: Using transactions in Stored procedures >
-- =============================================
Create PROCEDURE [dbo]. [Usp_procedurewithtransaction_demo]
As
Begin
Begin Try
Begin Transaction
Update Lock Set Locktypeid = 1 Where lockid = 32--This statement will be faulted, locktypeid to type int
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.