Capture PRINT messages from a stored procedure

Capture PRINT messages from a stored procedure

  • Comments 7

I recently helped with a customer issue where they had a long running stored procedure which output status messages periodically using PRINT statements. They wanted to capture these statements and output them into the SSIS log. Unfortunately, the Execute SQL Task doesn't support this (it's something we're considering for the future), but it's fairly easy to do through a script task.

Our stored procedure:

   1: CREATE PROCEDURE SPWithPrint
   2: AS
   3: BEGIN
   4:     print 'very important status information...'
   5: END
   6: GO

Our script:

   1: Public Sub Main()
   2:     Dim conn As New SqlConnection("server=(local);Integrated Security=SSPI;database=Test")
   3:  
   4:     AddHandler conn.InfoMessage, New SqlInfoMessageEventHandler(AddressOf OnInfoMessage)
   5:  
   6:     conn.Open()
   7:  
   8:     Dim cmd As New SqlCommand()
   9:     cmd.Connection = conn
  10:     cmd.CommandType = CommandType.StoredProcedure
  11:     cmd.CommandText = "[SPWithPrint]"
  12:  
  13:     cmd.ExecuteNonQuery()
  14:  
  15:     conn.Close()
  16:  
  17:     Dts.TaskResult = Dts.Results.Success
  18:  
  19: End Sub
  20:  
  21: Private Sub OnInfoMessage(ByVal sender As Object, ByVal args As System.Data.SqlClient.SqlInfoMessageEventArgs)
  22:     Dim sqlEvent As System.Data.SqlClient.SqlError
  23:     For Each sqlEvent In args.Errors
  24:         Dts.Events.FireInformation(sqlEvent.Number, sqlEvent.Procedure, sqlEvent.Message, "", 0, False)
  25:     Next
  26: End Sub

The print statements return the messages as InfoMessage events, which we catch with our handler and turn into SSIS information events.

When we run the package, we can see the message from the stored procedure in our progress window...

image

We're looking into adding this functionality to the Execute SQL Task as well, but hopefully this is an acceptable alternative until then.

Leave a Comment
  • Please add 6 and 5 and type the answer here:
  • Post
  • OK..I attempted this, but...the script never finishes..When I execute the stored proc from query analyzer it takes about 30 minutes, when I attempt the above, the task never finishes, and i never get any print statements. Any ideas?

    By the way, the same behavior occurs when I execute the stored proc using an Execute SQL Task (the task never finishes)...

    HELP!

  • Hi Johnny,

    I'd suggest running SQL Profiler when executing the package to see whats happening. You should be able to get an idea of what's going on, and where it's hanging.

    ~Matt

  • Matt,

    Thanks for your reply...I got the script to run, but the print statements didn't come up until after the stored procedure finished (basically, after the script completed). What am I doing wrong?

  • I don't think you're doing anything wrong - I think this is the standard behavior for the SqlInfoMessageEventHandler. At first I thought it was a limitation with the VSA scripting, but if I run the same code from a VB.Net console app, it behaves the same way.

  • Ok...thank you..I guess there's no way to get messages "as they happen" - that would be the most ideal - Something for MSFT to think about, I guess...

  • what type of Dts ??

      Dts.TaskResult = Dts.Results.Success

  • Any updates on this? Just curious...

Page 1 of 1 (7 items)