SQL transaction usage begin TRAN,COMMIT Tran and rollback TRAN usage _mssql

Source: Internet
Author: User
Tags commit rollback
The specific usage is as follows:
Copy Code code as follows:

-- =============================================
--Author:cynimoon
--Create date:2009-10-09
--Description: sample Stored Procedure
-- =============================================
--EXEC Test_proc ' literature comprehensive ', ' including history, geography, Politics ', ' politics ', ' one of the texts '
CREATE PROCEDURE [dbo]. [Test_proc]
@A_Name NVARCHAR,--Name of table A
@A_Remark NVARCHAR (4000),--A table Note
@B_Name NVARCHAR (s),--Name of table B
@B_Remark NVARCHAR (4000)--table B notes
As
BEGIN TRY
BEGIN TRAN
--Insert data in Table A
INSERT into [dbo]. A
([A_name]
, [A_remark])
VALUES
(@A_Name
, @A_Remark)
--Insert data in table B
INSERT into [dbo]. B
([a_id]
, [B_name]
, [B_remark])
VALUES
(@ @IDENTITY--Returns the last-inserted identity value
, @B_Name
, @B_Remark)
COMMIT TRAN
End TRY
BEGIN CATCH
ROLLBACK TRAN
INSERT into [dbo]. [ErrorLog]
([El_procedure]--Exception stored procedure name
, [El_operatetime])--Report Abnormal time
VALUES
(' Test_proc '
, CONVERT (Datetime,getdate (), 20))
End CATCH

Note: 1. The @ @IDENTITY function is to return the last identity value that was inserted.
2. I add a table in rollback TRAN that specifically records exceptions so that they occur at the time of the exception and the name of the stored procedure that reported the exception.
Original link:
Begin Tran can be understood as creating a new restore point.
Commit Tran Commit this change initiated from BEGIN Tran
Rollback TRAN represents a restore to the previous restore point.

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.