Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • Try Catch in SQL

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 30
    Comment on it

    SQL Server has an exception model to handle exceptions and errors that occurs in SQL statements. To handle exception in Sql Server we have TRY..CATCH blocks. In Sql Server, against a Try block we can have only one CATCH block.


    Ex;
    
         BEGIN TRY
        --T-SQL statements
        --or T-SQL statement blocks
        END TRY
        BEGIN CATCH
        --T-SQL statements
        --or T-SQL statement blocks
        END CATCH 
    


    This example shows to use try catch block in your sql queries


       BEGIN TRY
        DECLARE @num INT, @msg varchar(200)
        ---- Divide by zero to generate Error
        SET @num = 5/0
        PRINT 'This will not execute'
        END TRY
        BEGIN CATCH
        PRINT 'Error occured that is'
        set @msg=(SELECT ERRORMESSAGE())
        print @msg;
        END CATCH
    
    SQL

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: