@@ERROR (Transact-SQL)@@ERROR (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
最後に実行した Transact-SQLTransact-SQL ステートメントのエラー番号を返します。Returns the error number for the last Transact-SQLTransact-SQL statement executed.
Transact-SQL 構文表記規則
Transact-SQL Syntax Conventions
構文Syntax
@@ERROR
注意
SQL Server 2014 以前の Transact-SQL 構文を確認するには、以前のバージョンのドキュメントを参照してください。To view Transact-SQL syntax for SQL Server 2014 and earlier, see Previous versions documentation.
戻り値の型Return Types
整数 (integer)integer
解説Remarks
直前の Transact-SQLTransact-SQL ステートメントでエラーが発生しなかった場合は、0 が返されます。Returns 0 if the previous Transact-SQLTransact-SQL statement encountered no errors.
直前のステートメントでエラーが発生した場合は、エラー番号が返されます。Returns an error number if the previous statement encountered an error. エラーが sys.messages カタログ ビューのエラーであった場合、そのエラーに対応する sys.messages.message_id 列の値が @@ERROR に含まれます。If the error was one of the errors in the sys.messages catalog view, then @@ERROR contains the value from the sys.messages.message_id column for that error. @@ERROR エラー番号に関連付けられているテキストは、sys.messages で表示できます。You can view the text associated with an @@ERROR error number in sys.messages.
各ステートメントの実行時に @@ERROR はクリアおよびリセットされるので、ステートメントの検証を行った直後にこの変数の値を調べるか、または後で調べることができるようローカル変数に保存してください。Because @@ERROR is cleared and reset on each statement executed, check it immediately following the statement being verified, or save it to a local variable that can be checked later.
TRY...CATCH 構造を使用してエラーを処理します。Use the TRY...CATCH construct to handle errors. TRY...CATCH 構造では、その他のシステム関数 (ERROR_LINE、ERROR_MESSAGE、ERROR_PROCEDURE、ERROR_SEVERITY、ERROR_STATE) もサポートされます。これらの関数では、@@ERROR よりも多くのエラー情報が返されます。The TRY...CATCH construct also supports additional system functions (ERROR_LINE, ERROR_MESSAGE, ERROR_PROCEDURE, ERROR_SEVERITY, and ERROR_STATE) that return more error information than @@ERROR. また、ERROR_NUMBER 関数もサポートされます。この関数では、エラーが発生したステートメントの直後のステートメントでエラー番号を返すなどの操作を行えます。TRY...CATCH also supports an ERROR_NUMBER function that is not limited to returning the error number in the statement immediately after the statement that generated an error. 詳細については、「 TRY...CATCH (Transact-SQL)」を参照してください。For more information, see TRY...CATCH (Transact-SQL).
例Examples
A.A. @@ERROR を使用して特定のエラーを検出するUsing @@ERROR to detect a specific error
次の例では、@@ERROR
を使用して、UPDATE
ステートメントに CHECK 制約の違反 (エラー #547) がないかどうかを調べます。The following example uses @@ERROR
to check for a check constraint violation (error #547) in an UPDATE
statement.
USE AdventureWorks2012;
GO
UPDATE HumanResources.EmployeePayHistory
SET PayFrequency = 4
WHERE BusinessEntityID = 1;
IF @@ERROR = 547
BEGIN
PRINT N'A check constraint violation occurred.';
END
GO
B.B. @@ERROR を使用して条件的にプロシージャを終了するUsing @@ERROR to conditionally exit a procedure
次の例では、IF...ELSE
ステートメントを使用して、ストアド プロシージャ内で DELETE
ステートメントの後にある @@ERROR
をテストします。The following example uses IF...ELSE
statements to test @@ERROR
after an DELETE
statement in a stored procedure. @@ERROR
変数の値は、呼び出し元のプログラムに返されるリターン コードとして採用され、プロシージャ実行の成否を示します。The value of the @@ERROR
variable determines the return code sent to the calling program, indicating success or failure of the procedure.
USE AdventureWorks2012;
GO
-- Drop the procedure if it already exists.
IF OBJECT_ID(N'HumanResources.usp_DeleteCandidate', N'P') IS NOT NULL
DROP PROCEDURE HumanResources.usp_DeleteCandidate;
GO
-- Create the procedure.
CREATE PROCEDURE HumanResources.usp_DeleteCandidate
(
@CandidateID INT
)
AS
-- Execute the DELETE statement.
DELETE FROM HumanResources.JobCandidate
WHERE JobCandidateID = @CandidateID;
-- Test the error value.
IF @@ERROR <> 0
BEGIN
-- Return 99 to the calling program to indicate failure.
PRINT N'An error occurred deleting the candidate information.';
RETURN 99;
END
ELSE
BEGIN
-- Return 0 to the calling program to indicate success.
PRINT N'The job candidate has been deleted.';
RETURN 0;
END;
GO
C.C. @@ERROR を @@ROWCOUNT と共に使用するUsing @@ERROR with @@ROWCOUNT
次の例では、@@ERROR
と @@ROWCOUNT
を使用して、UPDATE
ステートメントの操作を検証します。The following example uses @@ERROR
with @@ROWCOUNT
to validate the operation of an UPDATE
statement. ここでは、@@ERROR
の値にエラーがないかどうかを調べ、@@ROWCOUNT
を使用して、テーブルの行を正しく更新できたかどうかを確認します。The value of @@ERROR
is checked for any indication of an error, and @@ROWCOUNT
is used to ensure that the update was successfully applied to a row in the table.
USE AdventureWorks2012;
GO
IF OBJECT_ID(N'Purchasing.usp_ChangePurchaseOrderHeader',N'P')IS NOT NULL
DROP PROCEDURE Purchasing.usp_ChangePurchaseOrderHeader;
GO
CREATE PROCEDURE Purchasing.usp_ChangePurchaseOrderHeader
(
@PurchaseOrderID INT
,@BusinessEntityID INT
)
AS
-- Declare variables used in error checking.
DECLARE @ErrorVar INT;
DECLARE @RowCountVar INT;
-- Execute the UPDATE statement.
UPDATE PurchaseOrderHeader
SET BusinessEntityID = @BusinessEntityID
WHERE PurchaseOrderID = @PurchaseOrderID;
-- Save the @@ERROR and @@ROWCOUNT values in local
-- variables before they are cleared.
SELECT @ErrorVar = @@ERROR
,@RowCountVar = @@ROWCOUNT;
-- Check for errors. If an invalid @BusinessEntityID was specified,
-- the UPDATE statement returns a foreign key violation error #547.
IF @ErrorVar <> 0
BEGIN
IF @ErrorVar = 547
BEGIN
PRINT N'ERROR: Invalid ID specified for new employee.';
RETURN 1;
END
ELSE
BEGIN
PRINT N'ERROR: error '
+ RTRIM(CAST(@ErrorVar AS NVARCHAR(10)))
+ N' occurred.';
RETURN 2;
END
END
-- Check the row count. @RowCountVar is set to 0
-- if an invalid @PurchaseOrderID was specified.
IF @RowCountVar = 0
BEGIN
PRINT 'Warning: The BusinessEntityID specified is not valid';
RETURN 1;
END
ELSE
BEGIN
PRINT 'Purchase order updated with the new employee';
RETURN 0;
END;
GO
参照See Also
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_NUMBER (Transact-SQL) ERROR_NUMBER (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)
@@ROWCOUNT (Transact-SQL) @@ROWCOUNT (Transact-SQL)
sys.messages (Transact-SQL) sys.messages (Transact-SQL)
エラーとイベントのリファレンス (データベース エンジン)Errors and Events Reference (Database Engine)