Error Handling - MarkMpn/Sql4Cds GitHub Wiki
SQL 4 CDS attempts to apply the same error handling semantics as SQL Server using the XACT_ABORT ON
setting.
If a statement raises an error, the rest of the batch is aborted. Errors can be raised manually with the
RAISERROR
or THROW
statements, or automatically by either errors encountered by the SQL 4 CDS engine itself or
those returned from the Dataverse server.
Warning
SQL 4 CDS does not apply implicit transactions to a statement, nor does it support explicit transactions. If an UPDATE
statement attempts to update 10 records but fails on the third record, the changes to the first two records will still
have been committed.
All errors have an associated error number, severity, state and message. All errors raised by SQL 4 CDS use the equivalent error numbers from SQL Server. Any errors generated by the Dataverse server will be mapped to the closest available SQL Server error number, but the error message produced by Dataverse will be preserved as it can contain more relevant detail to assist debugging.
Errors can be caught within a SQL script using the TRY
and CATCH
statements, and
the details of the error can be determined using the
@@ERROR
function or
the ERROR_LINE
,
ERROR_MESSAGE
,
ERROR_NUMBER
,
ERROR_PROCEDURE
,
ERROR_SEVERITY
and
ERROR_STATE
functions.
If you are using the ADO.NET provider you can also catch and view the errors in your own code:
try
{
cmd.CommandText = "INSERT INTO account (name) VALUES (@name)";
nameParam.Value = "A very long value";
cmd.ExecuteNonQuery();
}
catch (Sql4CdsException ex)
{
if (ex.Number == 8152)
{
// String or binary data would be truncated
// Shorten the value and try again
nameParam.Value = ((string)nameParam.Value).Substring(0, 50) + "...";
cmd.ExecuteNonQuery();
}
else
{
throw;
}
}