February 10, 2010 at 1:59 pm
I'm not sure if this is possible in SQL 2000 but I'm trying to store the Error Message in a local variable so I can output it or include it in an email. If for example I get a check constraint violation the error in EM or Query Analyzer would be displayed like so:
Msg 547, Level 16, State 0, Procedure sTest, Line 70
UPDATE statement conflicted with COLUMN CHECK constraint 'CK_tTest. The conflict occurred in database 'DB1', table 'tTable, column ID.
So I'd like to capture the whole message if possible. I know that @@ERROR returns the number but that doesn't give me the specifics. Is this possible?
Create Proc s_Test
as
decare @Error int
Insert Into tTable (ID)
select 1 as ID
SET @Error = @@Error
IF @Error <> 0
BEGIN
GOTO Error
END
Error:
SET @Body = @@Error Message goes here
exec CDOSend....
February 10, 2010 at 2:44 pm
Using BOL for 2000 look at this:
sysmessages
Contains one row for each system error or warning that can be returned by Microsoft® SQL Server™. SQL Server displays the error description on the user's screen.
For example:
SELECT Severity,description FROM master..sysmessages WHERE error = 5808
OR:
DECLARE @em VARCHAR(255)
SET @em = (SELECT description FROM master..sysmessages WHERE error = 5808)
SELECT @em
February 10, 2010 at 2:51 pm
bitbucket,
Thanks for the reply. I saw something about the sysmessages but I thought those were universal error messages and not the custom error messages that I was seeing. I'll give that a go and post back when I'm done.
February 10, 2010 at 2:55 pm
The only problem with this is that you do not get the actual error message... you will get the variable holders in the string in sysmessages, instead of the actual string with the variables substituted. i.e. "foreign key constraint violation: foreign key %s in table %s references table %s"
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
February 10, 2010 at 3:06 pm
hmmmm... is there a way to replace those placeholders to obtain the table name or the constraint that was violated?
February 10, 2010 at 3:16 pm
and not the custom error messages that I was seeing.
Refer to WayneS post, the "custom" message you receive is because the place holders described in WayneS post are replaced with the actual procedure text.
February 10, 2010 at 3:47 pm
There are two ways to replace the place holders ... read the following subjects in BOL
RAISERROR and/or FORMATMESSAGE.
It will not be easy and may require a great deal of code, since the sequence and number of parameters needed to be passed depends on the particular error message.
February 11, 2010 at 6:33 am
Thanks for the replies.
I'm not using RaisError so it's all about sysmessages and placeholders for me. I've searched around and one article suggested logging those errors and reading the error log. I've quickly read about FormatMessage too. It's a pain regardless. At least I can now see what the error message is and I have detailed error handling when it errors to know what DML statment caused the problem so my problem is at least partially solved.
Thanks again. I appreciate the help!
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply