lundi 5 octobre 2015

MS SQL try catch ignored when executed by php mssql_query

I simplified my problem with a simple stored procedure in MS SQL 2005 with a try catch block.

It goes like that:

CREATE PROCEDURE testError

AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

    BEGIN TRY
    DECLARE @X INT
    ---- Divide by zero to generate Error
        SET @X = 1/0
        select 'Command after error in TRY block' AS retour
    END TRY
    BEGIN CATCH
        select 'Error Detected' AS retour
        return
    END CATCH
    select 'Command after TRY/CATCH blocks' AS retour
END

When I execute it in management studio, I get the following result: Error Detected, which is what I want.

But when I execute it in PHP with mssql_query, I get the following result: Command after error in TRY block

Why my catch block is not triggered when an SQL error is encountered?

I want SQL to stop further execution and go to the catch block, as it does in management studio.

Thanks a lot for your help!

Aucun commentaire:

Enregistrer un commentaire