了解 Database Engine 錯誤Understanding Database Engine Errors

適用於: 是SQL Server 是Azure SQL Database 是Azure SQL 資料倉儲 是平行處理資料倉儲 APPLIES TO: yesSQL Server yesAzure SQL Database yesAzure SQL Data Warehouse yesParallel Data Warehouse

下表描述 MicrosoftMicrosoft SQL Server Database EngineSQL Server Database Engine 所引發之錯誤的屬性。Errors raised by the MicrosoftMicrosoft SQL Server Database EngineSQL Server Database Engine have the attributes described in the following table.

attributeAttribute DescriptionDescription
錯誤號碼Error number 每一則錯誤訊息都有唯一的錯誤號碼。Each error message has a unique error number.
錯誤訊息字串Error message string 錯誤訊息包含錯誤原因的診斷資訊。The error message contains diagnostic information about the cause of the error. 許多錯誤訊息都有用來插入資訊 (例如產生錯誤的物件名稱) 的替代變數。Many error messages have substitution variables in which information, such as the name of the object generating the error, is inserted.
SeveritySeverity 嚴重性指出錯誤的嚴重程度。The severity indicates how serious the error is. 嚴重性低 (例如 1 或 2) 的錯誤是參考訊息或低階警告。Errors that have a low severity, such as 1 or 2, are information messages or low-level warnings. 嚴重性高的錯誤指出應該儘快處理的問題。Errors that have a high severity indicate problems that should be addressed as soon as possible. 如需有關嚴重性的詳細資訊,請參閱 Database Engine 錯誤嚴重性For more information about severities, see Database Engine Error Severities.
StateState 對於 Database EngineDatabase Engine,程式碼的多個點都可能會產生某些錯誤訊息。Some error messages can be raised at multiple points in the code for the Database EngineDatabase Engine. 例如,在許多不同情況下,都有可能產生 1105 錯誤。For example, an 1105 error can be raised for several different conditions. 每個產生錯誤的特定狀況,都會指派唯一的狀態碼。Each specific condition that raises an error assigns a unique state code.

檢視內含已知問題之資訊的資料庫時 (例如 MicrosoftMicrosoft 知識庫),可以使用狀態碼來判斷所記錄的問題與您遇到的錯誤是否相同。When you are viewing databases that contain information about known issues, such as the MicrosoftMicrosoft Knowledge Base, you can use the state number to determine whether the recorded issue is the same as the error you have encountered. 例如,如果知識庫文件描述狀態為 2 的 1105 錯誤,而您收到之 1105 錯誤訊息的狀態為 3,則錯誤原因可能不是文件中所報告的原因。For example, if a Knowledge Base Article describes an 1105 error that has a state of 2 and the 1105 error message you received had a state of 3, the error probably has a different cause than the one reported in the article.

MicrosoftMicrosoft 支援工程師也可以使用錯誤中的狀態碼,來找出原始程式碼中引發錯誤碼的位置。A MicrosoftMicrosoft support engineer can also use the state code from an error to find the location in the source code where that error code is being raised. 這項資訊可能會提供如何診斷問題的其他想法。This information might provide additional ideas on how to diagnose the problem.
程序名稱Procedure name 這是發生錯誤之預存程序或觸發程序的名稱。Is the name of the stored procedure or trigger in which the error has occurred.
行號Line number 指出批次、預存程序、觸發程序或函數中的哪個陳述式產生錯誤。Indicates which statement in a batch, stored procedure, trigger, or function generated the error.

Database EngineDatabase Engine 執行個體中的所有系統和使用者自訂的錯誤訊息都包含在 sys.messages 目錄檢視中。All system and user-defined error messages in an instance of the Database EngineDatabase Engine are contained in the sys.messages catalog view. 您可以使用 RAISERROR 陳述式,將使用者自訂的錯誤傳回給應用程式。You can use the RAISERROR statement to return user-defined errors to an application.

所有資料庫 API (例如 MicrosoftMicrosoft .NET Framework.NET Framework SQLClient 命名空間、ActiveX Data Objects (ADO)、OLE DB 和開放式資料庫連接 (ODBC)) 都會報告基本錯誤屬性。All database APIs, such as the MicrosoftMicrosoft .NET Framework.NET Framework SQLClient namespace, ActiveX Data Objects (ADO), OLE DB, and Open Database Connectivity (ODBC), report the basic error attributes. 這項資訊包括錯誤號碼和訊息字串。This information includes the error number and message string. 不過,並非所有 API 都會報告所有其他錯誤屬性。However, not all the APIs report all the other error attributes.

您可以在 Transact-SQLTransact-SQL 程式碼中,使用相關聯之 CATCH 區塊範圍內的 ERROR_LINE、ERROR_MESSAGE、ERROR_NUMBER、ERROR_PROCEDURE、ERROR_SEVERITY 和 ERROR_STATE 等函數,來取得在 TRY…CATCH 建構的 TRY 區塊範圍內出現之錯誤的相關資訊。Information about an error that occurs in the scope of the TRY block of a TRY...CATCH construct can be obtained in Transact-SQLTransact-SQL code by using functions such as ERROR_LINE, ERROR_MESSAGE, ERROR_NUMBER, ERROR_PROCEDURE, ERROR_SEVERITY, and ERROR_STATE in the scope of the associated CATCH block. 如需詳細資訊,請參閱 TRY...CATCH (Transact-SQL)For more information, see TRY...CATCH (Transact-SQL).

範例Examples

下列範例會查詢 sys.messages 目錄檢視,以傳回 Database EngineDatabase Engine 中具有英文文字 (1033) 之所有系統和使用者自訂錯誤訊息的清單。The following example queries the sys.messages catalog view to return a list of all system and user-defined error messages in the Database EngineDatabase Engine that have English text (1033).

SELECT  
    message_id,  
    language_id,  
    severity,  
    is_event_logged,  
    text  
  FROM sys.messages  
  WHERE language_id = 1033;  

如需詳細資訊,請參閱 sys.messages (Transact-SQL)For more information, see sys.messages (Transact-SQL).

另請參閱See Also

sys.messages (Transact-SQL) sys.messages (Transact-SQL)
RAISERROR (Transact-SQL) RAISERROR (Transact-SQL)
@@ERROR (Transact-SQL) @@ERROR (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_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)