RAISERROR
({msg_id | msg_str | @local_variable}
{, severity,
state}[, argument [,...n]])[WITH [LOG]
[NOWAIT]
[SETERROR]];
The RAISERROR
statement can return specific error message text or a custom error message that
has been stored in the sysmessages table. To return an error message stored in sysmessages,
DBA/Developer can pass the RAISERROR statement the error's message_id as the
first parameter. In this scenario, DBA/Developer could include each DML
statement within DBA/Developerr stored procedures in a TRY block, and use the
RAISERROR statement within the associated CATCH block to return the desired
information about the error. DBA/Developer could use the ERROR_PROCEDURE
function in the CATCH block to return the name of the procedure that generated
the error and the ERROR_SEVERITY function to return the error's severity level.
DBA/Developer should not implement an output parameter in each stored procedure
to return the error message number. In this scenario, some of DBA/Developerr
stored procedures execute multiple DML statements. Therefore, returning
information for a single error is not sufficient. DBA/Developer should not
check the value of @@ERROR after each stored procedure call because the @@ERROR
function returns the error number of only the most recently executed Transact-SQL
statement. The @@ERROR function returns a value of zero if the most recently executed
Transact-SQL statement ran without errors. If an error occurred that corresponds
to an error stored in the sysmessages table, the function returns the
message_id for the error. The value of @@ERROR is reset each time a new
Transact-SQL statement is executed. Therefore, DBA/Developer should call the
function immediately following the Transact-SQL statement for which DBA/Developer
want to inspect the error number. DBA/Developer should not add a custom error
message for each stored procedure to the sysmessages table using the
sp_addmessage system stored procedure. The sp_addmessage system stored
procedure creates a custom error message and stores the new message in the
sysmessages table in the master database. After a user- defined error message
is created with sp_addmessage, Transact-SQL constructs or applications can reference
it using the RAISERROR statement. In this scenario, adding custom error messages
with the sp_addmessage system stored procedure would define custom error message,
but not ensure these message were returned by the stored procedures. DBA/Developer
would still need to add code within the stored procedures to implement error
handling.
No comments:
Post a Comment