SQL transaction usage: in tran, commit tran, and rollback tran usage

Source: Internet
Author: User

The usage is as follows:
Copy codeThe Code is as follows:
-- ===================================================== ======
-- Author: cynimoon
-- Create date: 2009-10-09
-- Description: sample stored procedure
-- ===================================================== ======
-- EXEC TEST_PROC '文 ',' including history, geography, politics ', 'politics', and '文'
Create procedure [dbo]. [TEST_PROC]
@ A_Name NVARCHAR (20), -- name of Table
@ A_Remark NVARCHAR (4000), -- remarks in Table
@ B _Name NVARCHAR (20), -- table B Name
@ B _Remark NVARCHAR (4000) -- remarks in Table B
AS
BEGIN TRY
BEGIN TRAN
-- Insert data into Table
Insert into [dbo]. [A]
([A_Name]
, [A_Remark])
VALUES
(@ A_Name
, @ A_Remark)
-- Insert data into Table B
Insert into [dbo]. [B]
([A_ID]
, [B _Name]
, [B _Remark])
VALUES
(@ IDENTITY -- return the last inserted id value
, @ B _Name
, @ B _Remark)
COMMIT TRAN
END TRY
BEGIN CATCH
ROLLBACK TRAN
Insert into [dbo]. [ErrorLog]
([EL_Procedure] -- Name of the exception Stored Procedure
, [EL_OperateTime]) -- reports the exception time
VALUES
('Test _ proc'
, CONVERT (DATETIME, GETDATE (), 20 ))
END CATCH

Note: 1. @ IDENTITY is used to return the last inserted id value.
2. I added a special exception record table to rollback tran to determine the time when the exception occurred and the name of the stored procedure reporting the exception.
Original article:
Begin tran can be understood as a new restore point.
Commit tran submits the modification starting from tran in tran
Rollback tran indicates restoring to the previous Restore Point.

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.