There is a test SP which has one output parameter only:
IF OBJECT_ID('dbo.USP_SAMPLE_PROCEDURE', 'P') IS NOT NULL
DROP PROCEDURE dbo.USP_SAMPLE_PROCEDURE;
GO
CREATE PROCEDURE USP_SAMPLE_PROCEDURE
(
@Param1 int OUTPUT
)
AS
BEGIN
SET XACT_ABORT, NOCOUNT ON;
BEGIN TRY
SELECT @Param1 = 123;
;THROW 50001, 'Test error', 0;
END TRY
BEGIN CATCH
SELECT @Param1 = 5;
-- Echo error information to the caller.
;THROW;
END CATCH
END
As you can see, I set some value for the @Param1 in case of normal flow and in case of error.
What is interesting about my code is that I cannot get the value 5. Here is my call:
DECLARE @Param1 int;
BEGIN TRY
EXEC dbo.USP_SAMPLE_PROCEDURE @Param1=@Param1 OUTPUT;
SELECT @Param1 AS [FROM TRY]
END TRY
BEGIN CATCH
SELECT @Param1 AS [FROM CATCH]
END CATCH
The result is NULL instead of 5. What am I doing wrong?