Capture PRINT messages from a stored procedure

Published 03-25-2019 01:58 PM 1,281 Views
Not applicable
First published on MSDN on Sep 13, 2007

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...



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

%3CLINGO-SUB%20id%3D%22lingo-sub-387352%22%20slang%3D%22en-US%22%3ECapture%20PRINT%20messages%20from%20a%20stored%20procedure%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-387352%22%20slang%3D%22en-US%22%3E%0A%20%26lt%3Bmeta%20http-equiv%3D%22Content-Type%22%20content%3D%22text%2Fhtml%3B%20charset%3DUTF-8%22%20%2F%26gt%3B%3CSTRONG%3E%20First%20published%20on%20MSDN%20on%20Sep%2013%2C%202007%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CP%3EI%20recently%20helped%20with%20a%20customer%20issue%20where%20they%20had%20a%20long%20running%20stored%20procedure%20which%20output%20status%20messages%20periodically%20using%20PRINT%20statements.%20They%20wanted%20to%20capture%20these%20statements%20and%20output%20them%20into%20the%20SSIS%20log.%20Unfortunately%2C%20the%20Execute%20SQL%20Task%20doesn't%20support%20this%20(it's%20something%20we're%20considering%20for%20the%20future)%2C%20but%20it's%20fairly%20easy%20to%20do%20through%20a%20script%20task.%3C%2FP%3E%0A%20%20%3CP%3EOur%20stored%20procedure%3A%3C%2FP%3E%0A%20%20%3CDIV%3E%0A%20%20%20%3CDIV%3E1%3A%20CREATE%20PROCEDURE%20SPWithPrint%202%3A%20AS%203%3A%20BEGIN%204%3A%20print%20'very%20important%20status%20information...'%205%3A%20END%206%3A%20GO%3C%2FDIV%3E%0A%20%20%3C%2FDIV%3E%3CBR%20%2F%3E%3CP%3EOur%20script%3A%3C%2FP%3E%3CBR%20%2F%3E%3CDIV%3E%3CBR%20%2F%3E%3CDIV%3E1%3A%20Public%20Sub%20Main()%202%3A%20Dim%20conn%20As%20New%20SqlConnection(%22server%3D(local)%3BIntegrated%20Security%3DSSPI%3Bdatabase%3DTest%22)%203%3A%26nbsp%3B%204%3A%20AddHandler%20conn.InfoMessage%2C%20New%20SqlInfoMessageEventHandler(AddressOf%20OnInfoMessage)%205%3A%26nbsp%3B%206%3A%20conn.Open()%207%3A%26nbsp%3B%208%3A%20Dim%20cmd%20As%20New%20SqlCommand()%209%3A%20cmd.Connection%20%3D%20conn%2010%3A%20cmd.CommandType%20%3D%20CommandType.StoredProcedure%2011%3A%20cmd.CommandText%20%3D%20%22%5BSPWithPrint%5D%22%2012%3A%26nbsp%3B%2013%3A%20cmd.ExecuteNonQuery()%2014%3A%26nbsp%3B%2015%3A%20conn.Close()%2016%3A%26nbsp%3B%2017%3A%20Dts.TaskResult%20%3D%20Dts.Results.Success%2018%3A%26nbsp%3B%2019%3A%20End%20Sub%2020%3A%26nbsp%3B%2021%3A%20Private%20Sub%20OnInfoMessage(ByVal%20sender%20As%20Object%2C%20ByVal%20args%20As%20System.Data.SqlClient.SqlInfoMessageEventArgs)%2022%3A%20Dim%20sqlEvent%20As%20System.Data.SqlClient.SqlError%2023%3A%20For%20Each%20sqlEvent%20In%20args.Errors%2024%3A%20Dts.Events.FireInformation(sqlEvent.Number%2C%20sqlEvent.Procedure%2C%20sqlEvent.Message%2C%20%22%22%2C%200%2C%20False)%2025%3A%20Next%2026%3A%20End%20Sub%3C%2FDIV%3E%0A%20%20%3C%2FDIV%3E%3CBR%20%2F%3E%3CP%3EThe%20print%20statements%20return%20the%20messages%20as%20InfoMessage%20events%2C%20which%20we%20catch%20with%20our%20handler%20and%20turn%20into%20SSIS%20information%20events.%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EWhen%20we%20run%20the%20package%2C%20we%20can%20see%20the%20message%20from%20the%20stored%20procedure%20in%20our%20progress%20window...%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F99342iD597971D6B7E4DFC%22%20%2F%3E%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EWe're%20looking%20into%20adding%20this%20functionality%20to%20the%20Execute%20SQL%20Task%20as%20well%2C%20but%20hopefully%20this%20is%20an%20acceptable%20alternative%20until%20then.%3C%2FP%3E%0A%20%0A%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-387352%22%20slang%3D%22en-US%22%3EFirst%20published%20on%20MSDN%20on%20Sep%2013%2C%202007%20I%20recently%20helped%20with%20a%20customer%20issue%20where%20they%20had%20a%20long%20running%20stored%20procedure%20which%20output%20status%20messages%20periodically%20using%20PRINT%20statements.%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-387352%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3Eexecute%20sql%20task%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Escript%20task%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Version history
Last update:
‎Mar 25 2019 01:58 PM
Updated by: