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.