2009-06-23 4 views
22

Ho uno sp in SQL Server che restituisce gli errori -4Valore restituito da un proc memorizzato su errore

Cosa significa -4? C'è un tavolo da qualche parte che spiega quali sono i possibili valori di ritorno?


Ci deve essere una serie

Per esempio

declare @RetVal int 
EXEC @RetVal = stpTest 
select @RetVal 

dove stpTest è "SELECT 1/0" restituisce -6.

-6 deve significare qualcosa!


Lo sp che ha restituito -4 contiene solo istruzioni UPDATE e SELECT INTO.

In nessun momento fa 'SELECT -4', quindi come posso "scoprire che cosa significa -4 in quella particolare stored procedure"?

Inoltre, se non esiste uno standard, allora perché un errore di divisione per zero restituisce sempre -6?


Se si dispone di un SP che non restituisce nulla vale a dire che non ha istruzioni SELECT in esso e si fare:

declare @RetVal int 
EXEC @RetVal = yourSPName 

Poi @RetVal avrà un valore pari a 0.

Se c'è un errore, @RetVal sarà un valore diverso da zero, ad esempio se l'unica cosa che fa sp è "SELECT 1/0", quindi @RetVal sarà -6.

provare e vedere

La mia domanda è che cosa significano questi valori di ritorno? Devono avere un significato logico!

+0

Come indicato più volte di seguito, non ci sono standard. Se si dispone della fonte dello sproc, provare le procedure di debug standard. – Adrien

+0

Quale versione di SQL Server stai usando? Modifica la domanda con un esempio completo, mostrando la stored procedure, l'esecuzione e il risultato. –

risposta

8

Non esiste uno standard per i codici di ritorno. Dovrai scoprire cosa significa -4 in quella particolare stored procedure. In effetti, non tutti i codici di ritorno sono errori.


EDIT: contro-esempio

SET ANSI_NULLS ON 
GO 

SET QUOTED_IDENTIFIER ON 
GO 

CREATE PROCEDURE [dbo].[RetValTest] 
AS 
BEGIN 
    select 1/0; 
END 

GO 

Esecuzione:

DECLARE @return_value int 

EXEC @return_value = [dbo].[RetValTest] 

SELECT 'Return Value' = @return_value 

GO 

Risultato:

Msg 8134, Level 16, State 1, Procedure RetValTest, Line 9 
Divide by zero error encountered. 

Questo è con SQL Server 2008.


Alcune ricerche suggeriscono che questo comportamento può essere lasciato da SQL Server 6.0. Se questo è il caso, allora puoi decidere da solo quanto è probabile che sia affidabile, dato che hanno smesso di documentarlo (e hanno smesso di garantirne l'accuratezza) tanto tempo fa.


mia "ricerca" è dovuto, con i ringraziamenti, a SQL Server MVP Tibor Karaszi. La sua fonte è Books Online per SQL Server 6.5. In "" Control Language-Of-Flow "RETURN", ha trovato

"SQL Server reserves 0 to indicate a successful return and reserves negative values from - 1 through - 99 to indicate different reasons for failure. If no user-defined return value is provided, the SQL Server value is used. User-defined return status values should not conflict with those reserved by SQL Server. The values 0 through -14 are currently in use.
+0

Ah ok, con SQL Server 2005 è un po 'diverso. RetValTest fornisce lo stesso messaggio di errore ma "SELECT" Valore restituito "= @return_value" dà un -6 –

+0

con sql2005 nella scheda messaggi si ottiene quell'errore, nella scheda risultati ottieni -6 - vedi la mia risposta sotto –

+0

appena eseguita la tua sopra l'esempio su SQL 2008 e ottenuto il valore di ritorno di -6 sotto l'errore di divisione per zero. provare le seguenti Query> risultati a> Risultati in testo ed eseguire di nuovo, il valore di ritorno è più evidente allora ... Oppure si potrebbe provare a guardare la scheda Risultati come suggerito nel mio precedente commento. Detto questo, forse versioni diverse/ambienti diversi potrebbero farci vedere risposte diverse, che lascia ancora valida la domanda originale. cioè da dove viene -6? –

-1

Non sono sicuro che c'è un modo per conoscere questo senza chiedere gli sviluppatori di SQL Server. È un livello inferiore rispetto al processo memorizzato che valuta ciò che lo stai inviando e genera quel codice di ritorno. A meno che tu non abbia specificatamente un RETURN -4 nel tuo codice, probabilmente proviene dal parser SQL.

3

Credo che la questione dovrebbe essere modificato a chiedere -

What are the default return values if you dont have a RETURN statement in your stored proc?

Qualcosa ho trovato è stato da questo link www.redware.com/handbooks/sql_server_handbook/sql_server_stored_procedures.html

SQL Server will default the return value to zero. The returned values are typically used to return a status flag from the stored procedure with a non-zero value usually indicating failure during processing.

Returned values are difficult to access using ODBC their use is recommended only to return a success or failure of the stored procedure when communicating with other stored procedures.

Da questo link - sqlserverpedia.com/wiki/Stored_Procedures_-_Output_Parameters_&_Return_Values

The return values -99 through 0 are reserved for SQL Server internal use. You can create your own parameters that can be passed back to the calling program.

anche un altro collegamento (credo) di @Erland Sommarskog www.sommarskog.se/error-handling-I.html

Return Values from Stored Procedures

All stored procedures have a return value, determined by the RETURN statement. The RETURN statement takes one optional argument, which should be a numeric value. If you say RETURN without providing a value, the return value is 0 if there is no error during execution. If an error occurs during execution of the procedure, the return value may be 0, or it may be a negative number. The same is true if there is no RETURN statement at all in the procedure: the return value may be a negative number or it may be 0.

Whether these negative numbers have any meaning, is a bit difficult to tell. It used to be the case, that the return values -1 to -99 were reserved for system-generated return values, and Books Online for earlier versions of SQL Server specified meanings for values -1 to -14. However, Books Online for SQL 2000 is silent on any such reservations, and does not explain what -1 to -14 would mean.

With some occasional exception, the system stored procedures that Microsoft ships with SQL Server return 0 to indicate success and any non-zero value indicates failure.

Come ottenere informazioni Errore

anche se hai bisogno di trovare ciò che l'errore è (piuttosto che quello -6 mezzi) si potrebbe provare a mettere il vostro SQL in un tentativo di cattura, vale a dire.

begin try 

    select 1/0 as 'an error' 

end try 

begin catch 

    select ERROR_NUMBER() as 'ERROR_NUMBER', 
      ERROR_SEVERITY() as 'ERROR_SEVERITY', 
      ERROR_STATE() as 'ERROR_STATE', 
      LEFT(ERROR_PROCEDURE(),50) as 'ERROR_PROCEDURE', 
      ERROR_LINE() as 'ERROR_LINE' , 
      LEFT(ERROR_MESSAGE(),40) as 'ERROR_MESSAGE'  
end catch 
+0

@Steve: no, non è la risposta. Dove spiega questo -6? È probabile che questo rimanga immondizia in un registro come qualsiasi altra cosa. –

27

Se si dispone di un'istruzione RETURN con un valore di ritorno esplicito, questo è ovviamente il valore restituito.

Ma se non è presente un'istruzione RETURN, ma si verifica un errore durante l'esecuzione, il valore restituito è 10 meno il livello di gravità dell'errore. La divisione per zero è il livello 16, quindi il valore restituito è -6. Gli errori di autorizzazione sono tipici di livello 14, quindi il valore di ritorno è -4.

Come potete immaginare, questo non è terribilmente utile, ma questo: 0 è successo e tutto il resto è un errore.

3

For example

declare @RetVal int 
EXEC @RetVal = stpTest 
select @RetVal 

where stpTest is SELECT 1/0 returns -6.

-6 must mean something!

If you have an sp that does not return anything i.e. it doesn't have any select statements in it and you do:

declare @RetVal int 
EXEC @RetVal = yourSPName 

Then @RetVal will have a value of 0.

If there is an error then @RetVal will be a value other then zero, for example if the only thing your sp does is SELECT 1/0 then @RetVal will be -6.

Try it and see


Prima di tutto, grazie per preoccuparsi di forgiare un esempio che restituisce -6.

Ecco what the documentation says about -6:

-6 Miscellaneous user error occurred.

-6 potrebbe essere il codice più amorfa che SQL restituisce Server.
Perché? Perché il codice di errore -6 essenzialmente nasconde un errore più profondo nello stack di chiamate.

Dopo risoluzione di questo errore io, qui sono i miei suggerimenti per la risoluzione di questo errore:

  1. Se il DAL o il livello di applicazione genera questo errore, quindi eseguire il codice SQL in SQL Server Management Studio come l'utente dell'applicazione , non con il proprio ID server SQL. [Perché? Perché le tue autorizzazioni potrebbero non essere le stesse.]
  2. Rendi il codice SQL sotto controllo il più leggibile possibile. [Perché? Perché questo errore può essere in profondità all'interno dello stack di chiamate.]
  3. In caso contrario, commentare metà del codice. L'errore si sta ancora verificando? Commenta il 50% del codice rimanente. Risciacqua e ripeti.