How to get the error message rather than the code error

How to get the error message rather than the code error

am 19.06.2007 22:43:59 von tsalm

Hi,

After sending a request, I would get the possible error message.
Not the code @@error, nor the exact content of sysmessages, but the
message like it could be in the log file.

TIA,
TSalm

Re: How to get the error message rather than the code error

am 19.06.2007 23:21:23 von Erland Sommarskog

TSalm (tsalm@free.fr) writes:
> After sending a request, I would get the possible error message.
> Not the code @@error, nor the exact content of sysmessages, but the
> message like it could be in the log file.

"Sending a request", that sounds like you are issuing a call from a
client program. In that case you should be able to pick up the error
message. If you tell which client API you are using, I may even be
able to tell you how.

If you mean in a stored procedure, it depends on which version of SQL
Server you are on. If you are on SQL 2000, the answer is: you can't.

If you are on SQL 2005, you can use the new function error_message()
and its sisters: error_severity(), error_number(), error_state(),
error_procedure() and error_line(). But they only return data, if you
are in a CATCH handler, or a procedure called from a catch handler:

BEGIN TRY
-- Do something bad here
END TRY
BEGIN CATCH
SELECT error_message()
END CATCH


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downlo ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books .mspx