Sunday, July 9, 2006

SQL Server 2005 has try-catch

[This was originally posted at http://timstall.dotnetdevelopersjournal.com/sql_server_2005_has_trycatch.htm]

I used to do error checking in SQL Server by adding a check after each statement, something like this:

 if @@error != 0
    begin
    RAISERROR 50001 'Some error message'
    RETURN
end

This has obvious problems (lots of extra code, what if you miss an error message, etc...). However, SQL Server 2005 has try-catch statements!

BEGIN TRY
{ sql_statement | statement_block }
END TRY
BEGIN CATCH
{ sql_statement | statement_block }
END CATCH
[ ; ]

This is definitely a convenient thing.

No comments:

Post a Comment