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.