August 16, 2012 at 4:47 am
Hi,
Is there anyway to capture the output of raiserror() ?
Error Message is having placeholders %s %s .. so i need to capture the actuall error message which is thrown by raiseerror()..
Regards,
Santhosh.
August 16, 2012 at 5:16 am
something like this should work...
declare
@ErrorMessage NVARCHAR(4000),
@ErrorNumber INT,
@ErrorSeverity INT,
@ErrorState INT,
@ErrorLine INT,
@ErrorProcedure NVARCHAR(200)
begin try
begin Tran
.....
commit tran
end try
begin catch
begin try
ROLLBACK TRANSACTION
end try
begin catch
end catch
SELECT
@ErrorNumber = ERROR_NUMBER(),
@ErrorSeverity = ERROR_SEVERITY(),
@ErrorState = ERROR_STATE(),
@ErrorLine = ERROR_LINE(),
@ErrorProcedure = ISNULL(ERROR_PROCEDURE(), '-');
SELECT @ErrorMessage =
N'Error %d, Level %d, State %d, Procedure %s, Line %d, ' +
'Message: '+ ERROR_MESSAGE();
RAISERROR
(
@ErrorMessage,
16,
1,
@ErrorNumber, -- parameter: original error number.
@ErrorSeverity, -- parameter: original error severity.
@ErrorState, -- parameter: original error state.
@ErrorProcedure, -- parameter: original error procedure name.
@ErrorLine -- parameter: original error line number.
);
end catch
August 16, 2012 at 5:30 am
August 16, 2012 at 3:14 pm
Catch it from where? In T-SQL or an app language like C#?
If your supplying a severity level of 10 then it will not trip a T-SQL CATCH block. Your RAISERROR with severity 10 is equal to a PRINT statement.
If connecting to SQL Server from .NET you can capture everything coming from the INFO message stream which is where PRINT and warning (i.e. RAISERROR with severity 10 or less) messages are written. If you're using .NET look into the SqlInfoMessageEventHandler class.
There are no special teachers of virtue, because virtue is taught by the whole community.
--Plato
August 17, 2012 at 12:47 am
If you know message_ID and parameters can you quiery sys.messages and reconstruct the actual message?
--Vadim R.
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply