Posts

Showing posts from July, 2012

Is your TRY worth catching?

Image
A very useful error handling  TRY/CATCH  construct is widely used to catch all execution errors  that do not close the database connection. The biggest downside is that in the case of multiple errors the  TRY/CATCH  mechanism will only catch the last error. An example of this can be seen during a standard restore operation. In this example I attempt to perform a restore from a file that no longer exists. Two errors are being fired:  3201  and  3013 : Assuming that we are using the  TRY  and  CATCH  construct, the  ERROR_MESSAGE ()  function will catch the last message only: To workaround this problem you can prepare a temporary table that will receive the statement output. Execute the statement inside the  xp_cmdshell  stored procedure, connect back to the SQL Server using the command line utility  sqlcmd  and redirect it's output into the previously created temp table.  After receiving the output, you will need to parse it to understand whether the statement has fin