Click here to Skip to main content
16,012,843 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi,
how can we use try catch block in stored procedure in sql server 2005
it s implementation
Posted

check this out
http://msdn.microsoft.com/en-us/library/ms175976(v=SQL.90).aspx[^]

BEGIN TRY
   Try Statement 1
   Try Statement 2
   ...
   Try Statement M
END TRY
BEGIN CATCH
   Catch Statement 1
   Catch Statement 2
   ...
   Catch Statement N
END CATCH


if one of the statements causes an error in Try Block, control branches immediately to the start of the CATCH block

The following system functions are available in the CATCH block and can be used to determine additional error information

HTML
Function	            Description
___________________________________________________________________________________
ERROR_NUMBER()	       Returns the number of the error
ERROR_SEVERITY()       Returns the severity
ERROR_STATE()	       Returns the error state number
ERROR_PROCEDURE()      Returns the name of the stored procedure or trigger where 
                       the error occurred
ERROR_LINE()	       Returns the line number inside the routine that caused error
ERROR_MESSAGE()	       Returns the complete text of the error message. 
                       The text includes the values supplied for any substitutable
                       parameters, such as lengths, object names, or times
 
Share this answer
 
v2
Comments
Simon Bang Terkildsen 14-Sep-11 0:30am    
OP wrote
please,
i didnt ask 4 a link
i want the process of implementation
Simon Bang Terkildsen 14-Sep-11 0:32am    
That link is perfectly valid to answer your question. And not only would it solve your problem if you actually read it, but also allow you know where you can lookup such questions in the future.
Simon Bang Terkildsen 14-Sep-11 0:32am    
+5 for a perfectly valid answer.
[no name] 14-Sep-11 0:48am    
my 5! also
SQL
CREATE PROCEDURE MyStoredProcedure
BEGIN   
BEGIN TRY 
  -- code
END TRY 
 
BEGIN CATCH 
  -- code
END CATCH     
END 


TRY...CATCH (Transact-SQL)[^]
 
Share this answer
 
SQL
USE AdventureWorks2008R2;
GO

-- Check to see whether this stored procedure exists.
IF OBJECT_ID (N'usp_GetErrorInfo', N'P') IS NOT NULL
    DROP PROCEDURE usp_GetErrorInfo;
GO

-- Create procedure to retrieve error information.
CREATE PROCEDURE usp_GetErrorInfo
AS
    SELECT 
        ERROR_NUMBER() AS ErrorNumber
        ,ERROR_SEVERITY() AS ErrorSeverity
        ,ERROR_STATE() AS ErrorState
        ,ERROR_LINE () AS ErrorLine
        ,ERROR_PROCEDURE() AS ErrorProcedure
        ,ERROR_MESSAGE() AS ErrorMessage;
GO

-- SET XACT_ABORT ON will cause the transaction to be uncommittable
-- when the constraint violation occurs. 
SET XACT_ABORT ON;

BEGIN TRY
    BEGIN TRANSACTION;
        -- A FOREIGN KEY constraint exists on this table. This 
        -- statement will generate a constraint violation error.
        DELETE FROM Production.Product
            WHERE ProductID = 980;

    -- If the DELETE statement succeeds, commit the transaction.
    COMMIT TRANSACTION;
END TRY
BEGIN CATCH
    -- Execute error retrieval routine.
    EXECUTE usp_GetErrorInfo;

    -- Test XACT_STATE:
        -- If 1, the transaction is committable.
        -- If -1, the transaction is uncommittable and should 
        --     be rolled back.
        -- XACT_STATE = 0 means that there is no transaction and
        --     a commit or rollback operation would generate an error.

    -- Test whether the transaction is uncommittable.
    IF (XACT_STATE()) = -1
    BEGIN
        PRINT
            N'The transaction is in an uncommittable state.' +
            'Rolling back transaction.'
        ROLLBACK TRANSACTION;
    END;

    -- Test whether the transaction is committable.
    IF (XACT_STATE()) = 1
    BEGIN
        PRINT
            N'The transaction is committable.' +
            'Committing transaction.'
        COMMIT TRANSACTION;   
    END;
END CATCH;
GO
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900