ERROR_MESSAGE (Transact-SQL)ERROR_MESSAGE (Transact-SQL)
適用対象:Applies to: SQL ServerSQL Server (サポートされているすべてのバージョン)
SQL ServerSQL Server (all supported versions)
Azure SQL データベースAzure SQL Database
Azure SQL データベースAzure SQL Database
Azure SQL Managed InstanceAzure SQL Managed Instance
Azure SQL Managed InstanceAzure SQL Managed Instance
Azure Synapse AnalyticsAzure Synapse Analytics
Azure Synapse AnalyticsAzure Synapse Analytics
Parallel Data WarehouseParallel Data Warehouse
Parallel Data WarehouseParallel Data Warehouse
SQL ServerSQL Server (サポートされているすべてのバージョン)
SQL ServerSQL Server (all supported versions)
Azure SQL データベースAzure SQL Database
Azure SQL データベースAzure SQL Database
Azure SQL Managed InstanceAzure SQL Managed Instance
Azure SQL Managed InstanceAzure SQL Managed Instance
Azure Synapse AnalyticsAzure Synapse Analytics
Azure Synapse AnalyticsAzure Synapse Analytics
Parallel Data WarehouseParallel Data Warehouse
Parallel Data WarehouseParallel Data Warehouse
この関数は、TRY...CATCH 構造の CATCH ブロックが実行される原因となったエラーのメッセージ テキストを返します。This function returns the message text of the error that caused the CATCH block of a TRY...CATCH construct to execute.
Transact-SQL 構文表記規則
Transact-SQL Syntax Conventions
構文Syntax
ERROR_MESSAGE ( )
注意
SQL Server 2014 以前の Transact-SQL 構文を確認するには、以前のバージョンのドキュメントを参照してください。To view Transact-SQL syntax for SQL Server 2014 and earlier, see Previous versions documentation.
戻り値の型Return Types
nvarchar (4000)nvarchar(4000)
戻り値Return Value
ERROR_MESSAGE
は、CATCH ブロック内で呼び出されると、CATCH
ブロックが実行される原因となったエラー メッセージの全テキストを返します。When called in a CATCH block, ERROR_MESSAGE
returns the complete text of the error message that caused the CATCH
block to run. このテキストには、長さ、オブジェクト名、時刻など、置換可能なパラメーターに指定された値が含まれます。The text includes the values supplied for any substitutable parameters - for example, lengths, object names, or times.
ERROR_MESSAGE
は、CATCH ブロックの範囲外で呼び出されると、NULL を返します。ERROR_MESSAGE
returns NULL when called outside the scope of a CATCH block.
解説Remarks
ERROR_MESSAGE
は、CATCH ブロックのスコープ内の任意の場所で呼び出すことができます。ERROR_MESSAGE
supports calls anywhere within the scope of a CATCH block.
ERROR_MESSAGE
は、実行回数に関係なく、あるいは 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. エラーが発生したステートメントの直後のステートメントのエラー番号のみを返す、@@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.
CATCH
ブロックが入れ子になっている場合、ERROR_MESSAGE
は、CATCH
ブロックを参照した CATCH
ブロックのスコープに固有のエラー メッセージを返します。In nested CATCH
blocks, ERROR_MESSAGE
returns the error message specific to the scope of the CATCH
block that referenced that CATCH
block. たとえば、外側の TRY...CATCH 構造の CATCH
ブロックの中に TRY...CATCH
構造が含まれることがあります。For example, the CATCH
block of an outer TRY...CATCH construct could have an inner TRY...CATCH
construct. その内側の CATCH
ブロック内では、ERROR_MESSAGE
は内側の CATCH
ブロックを呼び出したエラーからのメッセージを返します。Inside that inner CATCH
block, ERROR_MESSAGE
returns the message from the error that invoked the inner CATCH
block. ERROR_MESSAGE
が外側の CATCH
ブロック内で実行される場合、外側の CATCH
ブロックを呼び出したエラーからのメッセージを返します。If ERROR_MESSAGE
runs in the outer CATCH
block, it returns the message from the error that invoked that outer CATCH
block.
例Examples
A.A. CATCH ブロックで ERROR_MESSAGE を使用するUsing ERROR_MESSAGE in a CATCH block
この例は、0 除算エラーを生成する SELECT
ステートメントを示しています。This example shows a SELECT
statement that generates a divide-by-zero error. CATCH
ブロックはエラー メッセージを返します。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
結果セットは次のようになります。Here is the result set.
-----------
(0 row(s) affected)
ErrorMessage
----------------------------------
Divide by zero error encountered.
(1 row(s) affected)
B.B. CATCH ブロックで、別のエラー処理ツールと一緒に ERROR_MESSAGE を使用するUsing ERROR_MESSAGE in a CATCH block with other error-handling tools
この例は、0 除算エラーを生成する SELECT
ステートメントを示しています。This example shows a SELECT
statement that generates a divide-by-zero error. CATCH
ブロックは、エラー メッセージと共にそのエラーに関する情報を返します。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
結果セットは次のようになります。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)
参照See 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)
エラーとイベントのリファレンス (データベース エンジン)Errors and Events Reference (Database Engine)