篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql Transaction Best Basic Basic Structure.sql相关的知识,希望对你有一定的参考价值。
CREATE PROCEDURE [dbo].[TransactionBestPractiseStructure]
AS
BEGIN
SET NOCOUNT ON;
DECLARE @trancount int;
SET @trancount = @@trancount;
BEGIN TRY
IF @trancount = 0 BEGIN TRANSACTION;
ELSE SAVE TRANSACTION usp_test;
-- (START) - TRANSACTION RELATED TASK/STUFF SHOULD BE WRITTEN HEREEEEEEEEEEE
SELECT * FROM tbl_student
-- (END)
IF @trancount = 0 COMMIT;
END TRY
BEGIN CATCH
DECLARE @error int, @message varchar(4000), @xstate int;
SELECT @error = ERROR_NUMBER(), @message = ERROR_MESSAGE(), @xstate = XACT_STATE();
IF @xstate = -1 ROLLBACK;
IF @xstate = 1 AND @trancount = 0 ROLLBACK;
IF @xstate = 1 AND @trancount > 0 ROLLBACK TRANSACTION usp_test;
RAISERROR ('usp_test error: %d: %s' , 16 , 1 , @error , @message);
END CATCH;
END
以上是关于sql Transaction Best Basic Basic Structure.sql的主要内容,如果未能解决你的问题,请参考以下文章