2010-06-03 18 views
8

Il client che chiama questo codice è limitato e può gestire solo i codici di ritorno dai proc memorizzati. Così, abbiamo modificato la nostra solita contratto per RETURN -1 in caso di errore e predefinita-RETURN 0 se nessun erroreSQL Server stored procedure return code oddity

Se il codice colpisce il blocco catch interno, quindi il codice di ritorno di default è -4 piuttosto che 0

C'è qualcuno sai da dove viene questo per favore? Con riferimento

Acclamazioni gbn

IF OBJECT_ID('dbo.foo') IS NOT NULL DROP TABLE dbo.foo 
GO 
CREATE TABLE dbo.foo (
    KeyCol char(12) NOT NULL, 
    ValueCol xml NOT NULL, 
    Comment varchar(1000) NULL, 
    CONSTRAINT PK_foo PRIMARY KEY CLUSTERED (KeyCol) 
) 
GO 

IF OBJECT_ID('dbo.bar') IS NOT NULL DROP PROCEDURE dbo.bar 
GO 
CREATE PROCEDURE dbo.bar 
    @Key char(12), 
    @Value xml, 
    @Comment varchar(1000) 
AS 
SET NOCOUNT ON 
DECLARE @StartTranCount tinyint; 
BEGIN TRY 
    SELECT @StartTranCount = @@TRANCOUNT; 

    IF @StartTranCount = 0 BEGIN TRAN; 

    BEGIN TRY 
     --SELECT @StartTranCount = 'fish' --generates an error and goes to outer CATCH 
     INSERT dbo.foo (KeyCol, ValueCol, Comment) VALUES (@Key, @Value, @Comment); 
    END TRY 
    BEGIN CATCH 
     IF ERROR_NUMBER() = 2627 --PK violation 
      UPDATE 
       dbo.foo 
      SET 
       ValueCol = @Value, Comment = @Comment 
      WHERE 
       KeyCol = @Key; 
     ELSE 
      RAISERROR ('Tits up', 16, 1); 
    END CATCH 

    IF @StartTranCount = 0 COMMIT TRAN; 
END TRY 
BEGIN CATCH 
    IF @StartTranCount = 0 AND XACT_STATE() <> 0 ROLLBACK TRAN; 
    RETURN -1 
END CATCH 
--Without this, we'll send -4 if we hit the UPDATE CATCH block above 
--RETURN 0 
GO 

--please run these **separately** 

--Run with RETURN 0 and fish line commented out 
DECLARE @rtn int 
EXEC @rtn = dbo.bar 'abcdefghijkl', '<foobar />', 'testing' 
SELECT @rtn; SELECT * FROM dbo.foo 
GO 

DECLARE @rtn int 
EXEC @rtn = dbo.bar 'abcdefghijkl', '<foobar2 />', 'testing2' 
--updated OK but we get @rtn = -4 
SELECT @rtn; SELECT * FROM dbo.foo 
GO 

--uncomment fish line 
DECLARE @rtn int 
EXEC @rtn = dbo.bar 'abcdefghijkl', '<foobar />', 'testing' 
--Hit outer CATCH, @rtn = -1 as expected 
SELECT @rtn; SELECT * FROM dbo.foo 
+0

perché assegni "pesce" a un minuscolo? –

+0

Per forzare un'eccezione per testare il blocco CATCH esterno. Vedere l'ultimo EXEC ... – gbn

+0

scusate già rimosso il mio commento prima di aver visto la vostra risposta. (Il mio commento era falso in ogni caso :) –

risposta

10

a giocare in giro con la procedura, posso ottenere un ritorno -6, se inserisco un null in foo.KeyCol e rimuovere il RAISERROR nella cattura interiore. Questo è qualcosa che sta facendo SQL Server, ed è documentato qui: Return Values from Stored Procedures.

+1

Ho letto anche questo articolo ... – gbn

+0

@gbn, è stato fornito qui in questa domanda a cui hai anche risposto: http://www.sommarskog.se/error-handling-I.html#returnvalue –

+1

[Erland qui] (http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/b39cb2e5-3d49-4266-84dc-565b11ad0ffa/) dice che il valore restituito è 10 meno il livello di gravità dell'errore ma non riesco a vederlo menzionato nell'articolo collegato. –

Problemi correlati