Managing a Transaction Inside a Stored Procedure : Procedure « Procedure Function « SQL Server / T-SQL Tutorial






11> CREATE PROC usp_MyProc
12> AS
13> DECLARE
14>   @TranCount  int
15> SET
16>   @TranCount = @@TRANCOUNT
17> IF @TranCount > 0
18>   SAVE TRAN usp_MyProc             -- No existing transaction
19> ELSE
20>   BEGIN TRAN usp_MyProc            -- Transaction in progress
21> --do work here ...
22> IF @@ERROR > 0
23> BEGIN                              -- Failure
24>   RAISERROR ('usp_MyProc - Bailing out. ', 16, 1)
25>   ROLLBACK TRAN usp_MyProc
26>   RETURN
27> END
28> ELSE IF @Trancount = 0             -- Started our own transaction
29>   COMMIT TRAN usp_MyProc           -- Success
30> GO
1>
2> drop procedure usp_MyProc
3> GO








21.8.Procedure
21.8.1.calls the procedure
21.8.2.Declare a variable in a procedure
21.8.3.Stored Procedures as Parameterized Views
21.8.4.PRINT information out of a PROCEDURE
21.8.5.Procedure as a view
21.8.6.Return a value out of a procedure
21.8.7.Returning Values from a View as Stored Procedure Output Parameters
21.8.8.Filtering for Null Values with a Stored Procedure
21.8.9.Managing a Transaction Inside a Stored Procedure
21.8.10.Encrypting a Stored Procedure