SQL Error handling: All about

SQL Server

How to apply “try/catch” in query?

  • SQL Server[sql]Begin try
    BEGIN TRANSACTION;
    DELETE___ WHERE___
    End try
    Begin catch
    if @@error <> 0 goto err
    else
    begin
    COMMIT TRANSACTION;
    end
    End catch

    err:
    ROLLBACK
    [/sql]

How to get error message from “TRY.. CATCH..”

[sql]
BEGIN TRY
— Generate a divide-by-zero error.
SELECT 1/0;
END TRY
BEGIN CATCH
SELECT ERROR_MESSAGE() AS ErrorMessage;
END CATCH;
[/sql]

Be the first to comment

Leave a Reply

Your email address will not be published.


*