Impact on the increment key SQL Server transaction rollback

When SQL Server transaction rollback is deleted from the original insertion results in value-added, that is, before you roll back the data you insert a lead increment key plus 1, plus the state after rollback or 1

 

- getting the current operating values of the last inserted identity column:
SELECT @@ the IDENTITY
- To obtain the value of the last column of the identity of a table:
SELECT IDENT_CURRENT ( 'table')

- If you want to simulate throwing an exception can RAISERROR 

--RAISERROR ( 'description of the error', the error severity code error identification, the error value of the parameter description (this may be a plurality), other parameters)

--PRINT 'abnormal, Error Number:' + convert (varchar, error_number ()) + ', the error message:' + error_message ()

BEGIN TRAN Tran_Test; - starts a transaction
the DECLARE @tran_error the INT;
the SET @tran_error = 0;
the BEGIN TRY
the INSERT the INTO the Table
the RAISERROR ( 'reference document has been modified, the operation fails!', 16,. 1);
the END TRY
the BEGIN the CATCH
the PRINT 'appears exception, error number: '+ CONVERT (VARCHAR, ERROR_NUMBER ()) +', the error message: '
+ ERROR_MESSAGE ();
the SET @tran_error = @tran_error +. 1;
the END the CATCH;
the IF (@tran_error> 0)
the BEGIN
- Executive error, roll back the transaction
rOLLBACK TRAN;
the PRINT; '!'
END;
ELSE
BEGIN
- no exceptions, commit the transaction
COMMIT TRAN;
'!' the PRINT;
END;

SELECT SCOPE_IDENTITY();
SELECT @@IDENTITY;

Guess you like

Origin www.cnblogs.com/wangboke/p/11059647.html