HI WELCOME TO SIRIS

SQL Server Exceptions Working

Leave a Comment
SQL Server has an exception model to handle exceptions and errors that occurs in T-SQL statements. Exception handling in Sql Server is like as exception handling in other programming language. To understand exception handling, first we need to know how many types of exception we have in Sql Server.

Types of Exceptions

  1. Statement-Level Exception

    This type of exception aborts only the current running statement within a batch of T-SQL statements. The rest of the T-SQL statements will execute successfully if they have no exceptions. Let us see the below example.

    1. --Batch

    2. SELECT POWER(4, 28)

    3. PRINT 'This statement will execute'

    4. GO


  2. Batch-Level Exception

    This type of exception aborts only the batch in which exception occurs. The rest of the batches will execute successfully if they have no exceptions. The statement in which exception occurs will be aborted and the remaining T-SQL statements within the batch will also stopped.

    1. --First Batch

    2. DECLARE @var DECIMAL;

    3. set @var= CONVERT(DECIMAL, 'xyz')

    4. PRINT @var

    5. PRINT 'This statement will not execute'

    6. GO

    7. --Second Batch

    8. DECLARE @var DECIMAL;

    9. set @var= CONVERT(DECIMAL, '12.35')

    10. PRINT @var

    11. PRINT 'This statement will execute'

    12. GO


  3. Parsing and Scope-Resolution Exception

    This types of exception occurs during the parsing and during the scope-resolution phase of compilation. This exception appears to behave just like batch-level exceptions. However, this has a little different behavior.
    If the exception occurs in the same scope of the batch, it behaves just like a batch-level exception.If the exception occurs in a lower level of scope of the batch, it behaves just like statement-level exception.
    Parsing Exception

    1. --Parsing Error

    2. SELECTEmpID,Name FROM Employee

    3. PRINT 'This statement will execute'

    4. GO



    1. --For Successfully execution we need to executed select statement as dynamic SQL using the EXEC function

    2. EXEC('SELECTEmpID,Name FROM Employee')

    3. PRINT 'This statement will execute'

    4. GO


    Scope Resolution Exception

    1. --First Create a procedure

    2. CREATE PROCEDURE usp_print

    3. AS

    4. BEGIN

    5. Select * from tbl

    6. END

    7. GO



    1. --Now execute above created procedure in batch

    2. EXEC usp_print

    3. PRINT 'This statement will execute'

    4. GO

    5. --Since the stored procedure creates a new scope. Hence rest statement will be executed


Summary
In this article I try to explain how types of Exception in Sql Server with example. I hope after reading this article your will be aware of exceptions in Sql Server. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.