PRINT statement output

Hi,

I use PRINT statements to display final message in my stored procedure. Is any way to catch PRINT output (it is Messages in SSMS)? I need to send email if SP returns any messages once it finishes.

Thanks

March 23rd, 2015 12:30pm

Declare a OUTPUT variable and then assign the message to the variable. So you can send an email if the message exists.
Free Windows Admin Tool Kit Click here and download it now
March 23rd, 2015 12:42pm

you can append messages to a variable and finally assign variable value to an OUTPUT parameter created in the procedure. Then through OUPUT parameter you can get it outside
March 23rd, 2015 12:46pm

You can only catch automatically if SQLCMD is used for the script execution (-i and -o parameters).

SQLCMD: https://msdn.microsoft.com/en-us/library/ms162773.aspx

Free Windows Admin Tool Kit Click here and download it now
March 23rd, 2015 12:49pm

PRINT is the not way to accomplish what you are looking for, you need to use RAISERROR. 

Please see:

https://msdn.microsoft.com/en-us/library/ms178592.aspx?f=255&MSPPError=-2147217396

March 23rd, 2015 1:29pm

Hi Jori5,

Usually the Message pane in SSMS shows the rows that affected by the statments and syntax or runtime error message.

May I know what kind of message is your Stored  Procedure(SP) expected to return?

For the affected rows, the message can be avoided by "SET NOCOUNT ON" or be captured by "SELECT @@ROWCOUNT".

For the error message, it can be captured by "SELECT @@ERROR", see more about Error Handling in SQL Server.

If what you'd like to capture is only the message in your own PRINT statements, you can follow the advice in above posts to declare an OUTPUT parameter and assign your final message to the parameter. see below link.
Returning Data by Using OUTPUT Parameters

If you have any question, feel free to let me

Free Windows Admin Tool Kit Click here and download it now
March 24th, 2015 2:34am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics