Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Capture SQL Exception output from stored procedure

$
0
0

Running against SQL Server 2008 R2.

I am attempting to capture the SQL exception output by executing a stored procedure which forces a divide by zero error ("select 1/0 as booboo").

I am using a SQLCommand object to execute the stored procedure.   No problem there.

I've defined a SqlInfoMessageEventHandler to simply write to a text file as follows:

$errout = [System.Data.SqlClient.SqlInfoMessageEventHandler] { Add-Content -Path $outfile -Value "$($_)" }

I've added the handler to the SQlConnection object as:

$SqlConnection.add_InfoMessage($errout)

As documented, the SqlInfoMessageEventHandler outputs PRINT output (Severity <= 10) just fine but I've been unable to determine how to access the output from a Severity 16 (generated by the div/0 error) error.

I saw elsewhere that I should be able to iterate through an $err object in the catch clause but have had no success there, besides I did not see where the $err object would capture the output anyway...

Anyone have any guidance on this?

TIA


Viewing all articles
Browse latest Browse all 6937

Trending Articles