Error Handling in T-SQL

  • Hi,

    Is it possible to retrieve Error Description in T-Sql just like @@Error.

    Also, is it possible to trap a sql server generated error. I mean to ask, if any constraint is violated (like primary key), how can I trap this error. I have an error condition immediately after this insert statement but, the execution stops immediately after the error, and rolling back the calling transaction. This inturn is causing another error warning when I try to rollback or commit the calling tran.

    I am adding all the necessary precautionary statements but just want to know if it is possible to trap these errors

    Thanks & Regards,

    Mitra

  • Ah welcome to the stone-age. Error handling in SQL Server is one of it's less attractive points, very limited.

    There appear to be two types of error, fatal and non fatal (with most server errors being terminal).

    For non-fatal errors TSQL allows processing to drop to the next line after the error, so you can do some recovery.

    Fatal errors just zap your application, no chance to rcover.

    I've found that if you use ADO to call the database, the error can be caught from the OLE object and you can at least hide it from your user and return somewhat more elegantly as the description, number etc are available to you


    I have found it is possible to please all of the people all of the time if you do exactly what they want. Harold Macmillan 1961

  • If use ADO components to communicate with SQL Server(i think you do) then you have a lot of methods to trap(or handle) errors that MSSQL raises. After you have catched some error in the error handler you can obtain error number, error message and collection of errors from Err and Connection objects.

    There's no need to use T-SQL features to trap errors. It's difficult way and it leads nowhere.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply