SQL Server Error Detail

 

System_CAPS_ICON_warning.jpg Warning

SQL Server Native Client (SNAC) is not supported beyond SQL Server 2012. Avoid using SNAC in new development work, and plan to modify applications that currently use it. The Microsoft ODBC Driver for SQL Server provides native connectivity from Windows to Microsoft SQL Server and Microsoft Azure SQL Database.

The SQL Server Native Client OLE DB provider defines the provider-specific error interface ISQLServerErrorInfo. The interface returns more detail about a SQL Server error and is valuable when command execution or rowset operations fail.

There are two ways to obtain access to ISQLServerErrorInfo interface.

The consumer may call IErrorRecords::GetCustomerErrorObject to obtain an ISQLServerErrorInfo pointer, as shown in the following code sample. (There is no need to obtain ISQLErrorInfo.) Both ISQLErrorInfo and ISQLServerErrorInfo are custom OLE DB error objects, with ISQLServerErrorInfo being the interface to use to obtain information of server errors, including such details as procedure name and line numbers.

// Get the SQL Server custom error object.  
if(FAILED(hr=pIErrorRecords->GetCustomErrorObject(  
   nRec, IID_ISQLServerErrorInfo,  
   (IUnknown**)&pISQLServerErrorErrorInfo)))  

Another way to get an ISQLServerErrorInfo pointer is to call the QueryInterface method on an already-obtained ISQLErrorInfo pointer. Note that because ISQLServerErrorInfo contains a superset of the information available from ISQLErrorInfo, it makes sense to go directly to ISQLServerErrorInfo through GetCustomerErrorObject.

The ISQLServerErrorInfo interface exposes one member function, ISQLServerErrorInfo::GetErrorInfo. The function returns a pointer to an SSERRORINFO structure and a pointer to a string buffer. Both pointers reference memory the consumer must deallocate by using the IMalloc::Free method.

SSERRORINFO structure members are interpreted by the consumer as follows.

MemberDescription
pwszMessageSQL Server error message. Identical to the string returned in IErrorInfo::GetDescription.
pwszServerName of the instance of SQL Server for the session.
pwszProcedureIf appropriate, the name of the procedure in which the error originated. An empty string otherwise.
lNativeSQL Server native error number. Identical to the value returned in the plNativeError parameter of ISQLErrorInfo::GetSQLInfo.
bStateState of a SQL Server error message.
bClassSeverity of a SQL Server error message.
wLineNumberWhen applicable, the line number of a stored procedure on which the error occurred.

Errors
RAISERROR (Transact-SQL)

Community Additions

ADD
Show: