Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Stored Procedure OUTPUT Parameter NULL After RAISERROR

$
0
0

Hi all.

I am trying to use an OUTPUT parameter in a stored procedure that returns a value which will be later used regardless if an error occurred in the procedure or not.  It is a trace collector XML parameter.

createprocedure dbo.output_par_test
(   
   @MessageLog       xml = nulloutput
)

When I later use the procedure in SSMS as a simple EXECUTE, everything seems to be working, and I receive a list of messages, including the one with error.

declare @msg xml;
exec dbo.output_par_test @MessageLog = @msg  output;
select @msg as TEST1_xml_Immediate_Execution;

BUT, when I use the same procedure in a TRY, then the value of the OUTPUT parameter is null in the CATCH!  Can't see what I am missing unless this is a BUG in "Microsoft SQL Server 2005 - 9.00.5292.00 (Intel X86)   Apr 13 2011 15:56:31   Copyright (c) 1988-2005 Microsoft Corporation  Enterprise Edition on Windows NT 5.2 (Build 3790: Service Pack 2)".

Please help with the problem.  Of course one could have returned a resultset just before rethrowing the error, but I wonder if there is something I am missing, or this is a bug, or intented - then why.

The code is below.

use tempdb;
go

create procedure dbo.output_par_test
(   
   @MessageLog       xml = null output
)
as
set nocount on;
declare @SQL nvarchar(max);
declare @isErr int;
declare @MsgLog table (
   typ  int,
   msg  varchar(max),
   src  sysname default object_name(@@procid),
   dttm datetime default getdate() );
set @isErr = 0;

begin try
	insert into @MsgLog (msg,typ) values('Message 1.', 1);
   
   raiserror('My Error', 11, 1);
   --raiserror('My Message', 1, 1);

	insert into @MsgLog (msg,typ) values('Finished, exiting', 1);
end try

begin catch
   declare @ErrorString varchar(max);
   set @ErrorString = 
      isnull(ERROR_MESSAGE(), 'message is NULL') + '
      Procedure ' + ERROR_PROCEDURE() + '
      Line ' + isnull(cast(ERROR_LINE() as varchar(max)), 'NULL') + '
      Error# ' + isnull(cast(ERROR_NUMBER() as varchar(max)), 'NULL') + '
      Severity ' + isnull(cast(ERROR_SEVERITY() as varchar(max)), 'NULL') + '
      State ' + isnull(cast(ERROR_STATE() as varchar(max)), 'NULL') + '. ';

   set @isErr = 1;
	insert into @MsgLog (msg,typ) values(@ErrorString, 9);
end catch
--------------------------------------------------------------------------------
set @MessageLog = (
   select typ as MessageTypeID, msg as MessageText, src as SourceName, dttm as RecordLoadDttm
   from @MsgLog for xml path
);
if @isErr = 1
  raiserror (@ErrorString, 11, 111);
--------------------------------------------------------------------------------
go

--------------------------------------------------------------------------------
-- Test 1
--------------------------------------------------------------------------------
declare @msg xml;
exec dbo.output_par_test @MessageLog = @msg  output;
select @msg as TEST1_xml_Immediate_Execution;
go

--------------------------------------------------------------------------------
-- Test 2
--------------------------------------------------------------------------------
declare @msg xml;
begin try
   exec dbo.output_par_test @MessageLog = @msg  output;
   select @msg as TEST2_xml_From_Try;
end try
begin catch
   select @msg as TEST2_xml_From_CATCH;
end catch
go

Thank you!


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>