Presented by Aaron Buma
Error Messages – How to get them
 CREATE objects that already exist
 Violating Foreign Keys
 Exceeding permissions
 Trying operations that aren’t allowed (PRINT in a UDF)
Error Messages – Basic layout
 Message Number
 Number 50,000 for custom messages (default)
 State – You can track error locations with custom errors
 Severities
 1-10: Status Info (Not Bad)
 11-16: Can be corrected by the user (Fixable)
 17-19: Errors with SQL Resources (Bad)
 20-25: System Problems, Fatal Errors (Really Bad)
Error Messages - Custom
 You create message and severity
 Only users with SysAdmin role or Alter Trace can use
WITH LOG – writes to error log
 You can have parameters in the text
 You can use Profiler to watch for them
 Watch for message number ‘50,000’ or a specific State
Error Handling
 Statement Termination - skip statement, but continue
 Scope Abortion - Exit this object, but continue
 Batch Abortion - Stop calling object
 Connection Termination – Disconnect, rollback open Trans
 ~ Severity > 20
Error Handling - XACT_ABORT
 If ON and in a transaction, it will rollback transaction
 SET XACT_ABORT ON / OFF
 Errors with Severity 11-16 cause Batch Abortion
Error Handling - TRY and CATCH
 BEGIN TRY
 Have your statements in the TRY, including BEGIN TRANs
 BEGIN CATCH
 Run code when error encountered
 You have access to the error properties
Demo

SQL: Error Messages and Error Handling

  • 1.
  • 2.
    Error Messages –How to get them  CREATE objects that already exist  Violating Foreign Keys  Exceeding permissions  Trying operations that aren’t allowed (PRINT in a UDF)
  • 3.
    Error Messages –Basic layout  Message Number  Number 50,000 for custom messages (default)  State – You can track error locations with custom errors  Severities  1-10: Status Info (Not Bad)  11-16: Can be corrected by the user (Fixable)  17-19: Errors with SQL Resources (Bad)  20-25: System Problems, Fatal Errors (Really Bad)
  • 4.
    Error Messages -Custom  You create message and severity  Only users with SysAdmin role or Alter Trace can use WITH LOG – writes to error log  You can have parameters in the text  You can use Profiler to watch for them  Watch for message number ‘50,000’ or a specific State
  • 5.
    Error Handling  StatementTermination - skip statement, but continue  Scope Abortion - Exit this object, but continue  Batch Abortion - Stop calling object  Connection Termination – Disconnect, rollback open Trans  ~ Severity > 20
  • 6.
    Error Handling -XACT_ABORT  If ON and in a transaction, it will rollback transaction  SET XACT_ABORT ON / OFF  Errors with Severity 11-16 cause Batch Abortion
  • 7.
    Error Handling -TRY and CATCH  BEGIN TRY  Have your statements in the TRY, including BEGIN TRANs  BEGIN CATCH  Run code when error encountered  You have access to the error properties
  • 8.