AspBucket offers ASP.NET, C#, VB, Jquery, CSS, Ajax, SQL tutorials. It is the best place for programmers to learn

Thursday 21 April 2016

How to use try catch statement in Stored Procedure?

We can use Try Catch Statement in our Stored procedure to handle error. In SQL there are no finally block like in C#. Let's start discussing it with an example.
Example
CREATE PROCEDURE TestProc
AS 
    BEGIN
 
        SET NOCOUNT ON ;

        PRINT 'Before Try Statement'
        BEGIN TRY
            PRINT 'First Statement in the TRY block'  
            --Write your Logic          
            PRINT 'Last Statement in the TRY block'
        END TRY
        BEGIN CATCH
            PRINT 'In CATCH Block'
        END CATCH
        PRINT 'After END CATCH'

    END
GO

Above code states that if the statements enclosed within the TRY block doesn’t result in any errors, then the control is not passed to the CATCH block else Catch block will execute.

Hope you like this articles. Please give suggestion in below comments. 

0 comments :

Post a Comment

  • Popular Posts
  • Comments