Try Catch for SQLServer T-SQL
April 07, 2008
Error handling in SQL Server T-SQL scripts is not always the most graceful thing to deal with. With the release of Microsoft SQL Server 2005 you can now use TRY / CATCH statements. Here's a simple example of try / catch on sqlserver:
BEGIN TRY -- try / catch requires SQLServer 2005 -- run your code here END TRY BEGIN CATCH PRINT 'Error Number: ' + str(error_number()) PRINT 'Line Number: ' + str(error_line()) PRINT error_message() -- handle error condition END CATCH
If you want to throw an exception with SQL Server you can use the RAISERROR() function, for example:
RAISERROR('Error Message', 18, 0);
Related Entries
- SQL Server Express 2005, Finally Installed - September 7, 2005
- I give up! Installing SQL Server 2005 Express - February 25, 2005
- Use varchar(max) instead of text in SQL Server - December 9, 2009
- Cheat Sheet for SQL Server - April 20, 2009
- DateFormat for SQL Server - December 5, 2008



add to del.icio.us



