Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Try/Catch syntax with the exact error
Message
 
 
To
05/03/2008 09:51:17
General information
Forum:
Microsoft SQL Server
Category:
Other
Miscellaneous
Thread ID:
01298895
Message ID:
01298904
Views:
25
>I have some Try/Catch syntaxes which allows me to print in the Messages window some specific message when an error happens. But, in that message, I also need to print the exact error. How can I obtain that at that level?

From BOL:
USE AdventureWorks;
GO

-- Verify that the stored procedure does not exist.
IF OBJECT_ID ('usp_GetErrorInfo', '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_PROCEDURE() as ErrorProcedure,
        ERROR_LINE() as ErrorLine,
        ERROR_MESSAGE() as ErrorMessage;
GO

BEGIN TRY
    -- Generate divide-by-zero error.
    SELECT 1/0;
END TRY
BEGIN CATCH
    -- Execute the error retrieval routine.
    EXECUTE usp_GetErrorInfo;
END CATCH;
GO
If it's not broken, fix it until it is.


My Blog
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform