ERROR_MESSAGE (Transact-SQL)ERROR_MESSAGE (Transact-SQL)
SQL Server
Azure SQL Database
Azure Synapse Analytics (SQL DW)
Almacenamiento de datos paralelos
SQL Server
Azure SQL Database
Azure Synapse Analytics (SQL DW)
Parallel Data Warehouse
Esta función devuelve el texto del mensaje del error que ha provocado la ejecución del bloque CATCH de una construcción TRY…CATCH.This function returns the message text of the error that caused the CATCH block of a TRY...CATCH construct to execute.
Convenciones de sintaxis de Transact-SQL
Transact-SQL Syntax Conventions
SintaxisSyntax
ERROR_MESSAGE ( )
Tipos devueltosReturn Types
nvarchar(4000)nvarchar(4000)
Valor devueltoReturn Value
Cuando se llama en un bloque CATCH, ERROR_MESSAGE
devuelve el texto completo del mensaje de error que ha provocado la ejecución del bloque CATCH
.When called in a CATCH block, ERROR_MESSAGE
returns the complete text of the error message that caused the CATCH
block to run. En el texto se incluyen los valores proporcionados para los parámetros sustituibles, como las longitudes, los nombres de objeto o las horas.The text includes the values supplied for any substitutable parameters - for example, lengths, object names, or times.
ERROR_MESSAGE
devuelve NULL si se llama desde fuera del ámbito de un bloque CATCH.ERROR_MESSAGE
returns NULL when called outside the scope of a CATCH block.
NotasRemarks
ERROR_MESSAGE
admite llamadas en cualquier lugar del ámbito de un bloque CATCH.ERROR_MESSAGE
supports calls anywhere within the scope of a CATCH block.
ERROR_MESSAGE
devuelve un mensaje de error relevante, con independencia de cuántas veces se ejecute o de dónde se ejecute dentro del ámbito del bloque CATCH
.ERROR_MESSAGE
returns a relevant error message regardless of how many times it runs, or where it runs within the scope of the CATCH
block. Esto contrasta con funciones como @@ERROR, que solo devuelve un número de error en la instrucción inmediatamente posterior a la que produjo el error.This contrasts with a function like @@ERROR, which only returns an error number in the statement immediately following the one that causes an error.
En los bloques CATCH
anidados, ERROR_MESSAGE
devuelve el mensaje de error específico del ámbito del bloque CATCH
al que hace referencia ese bloque CATCH
.In nested CATCH
blocks, ERROR_MESSAGE
returns the error message specific to the scope of the CATCH
block that referenced that CATCH
block. Por ejemplo, el bloque CATCH
de una construcción TRY...CATCH externa podría tener una construcción TRY...CATCH
interna.For example, the CATCH
block of an outer TRY...CATCH construct could have an inner TRY...CATCH
construct. Dentro de ese bloque interno CATCH
, ERROR_MESSAGE
devuelve el mensaje de error que invocó el bloque CATCH
interno.Inside that inner CATCH
block, ERROR_MESSAGE
returns the message from the error that invoked the inner CATCH
block. Si ERROR_MESSAGE
se ejecuta en el bloque CATCH
externo, devuelve el mensaje de error que invocó ese bloque CATCH
externo.If ERROR_MESSAGE
runs in the outer CATCH
block, it returns the message from the error that invoked that outer CATCH
block.
EjemplosExamples
A.A. Usar ERROR_MESSAGE en un bloque CATCHUsing ERROR_MESSAGE in a CATCH block
En este ejemplo se muestra una instrucción SELECT
que genera un error de división por cero.This example shows a SELECT
statement that generates a divide-by-zero error. El bloque CATCH
devuelve el mensaje de error.The CATCH
block returns the error message.
BEGIN TRY
-- Generate a divide-by-zero error.
SELECT 1/0;
END TRY
BEGIN CATCH
SELECT ERROR_MESSAGE() AS ErrorMessage;
END CATCH;
GO
El conjunto de resultados es el siguiente.Here is the result set.
-----------
(0 row(s) affected)
ErrorMessage
----------------------------------
Divide by zero error encountered.
(1 row(s) affected)
B.B. Usar ERROR_MESSAGE en un bloque CATCH con otras herramientas de control de errores.Using ERROR_MESSAGE in a CATCH block with other error-handling tools
En este ejemplo se muestra una instrucción SELECT
que genera un error de división por cero.This example shows a SELECT
statement that generates a divide-by-zero error. Junto con el mensaje de error, el bloque CATCH
devuelve información sobre ese error.Along with the error message, the CATCH
block returns information about that error.
BEGIN TRY
-- Generate a divide-by-zero error.
SELECT 1/0;
END TRY
BEGIN CATCH
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;
END CATCH;
GO
El conjunto de resultados es el siguiente.Here is the result set.
-----------
(0 row(s) affected)
ErrorNumber ErrorSeverity ErrorState ErrorProcedure ErrorLine ErrorMessage
----------- ------------- ----------- --------------- ---------- ----------------------------------
8134 16 1 NULL 4 Divide by zero error encountered.
(1 row(s) affected)
Consulte tambiénSee Also
sys.messages (Transact-SQL) sys.messages (Transact-SQL)
TRY...CATCH (Transact-SQL) TRY...CATCH (Transact-SQL)
ERROR_LINE (Transact-SQL) ERROR_LINE (Transact-SQL)
ERROR_MESSAGE (Transact-SQL) ERROR_MESSAGE (Transact-SQL)
ERROR_PROCEDURE (Transact-SQL) ERROR_PROCEDURE (Transact-SQL)
ERROR_SEVERITY (Transact-SQL) ERROR_SEVERITY (Transact-SQL)
ERROR_STATE (Transact-SQL) ERROR_STATE (Transact-SQL)
RAISERROR (Transact-SQL) RAISERROR (Transact-SQL)
@@ERROR (Transact-SQL) @@ERROR (Transact-SQL)
Referencia de errores y eventos (Motor de base de datos)Errors and Events Reference (Database Engine)
Comentarios
Cargando comentarios...