Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 capturing error message

Author  Topic 

lfmn
Posting Yak Master

141 Posts

Posted - 2002-06-27 : 16:44:20
when I run the following statement:
insert authors (au_id)
values ('172-32-1176')

I get the following error:
Server: Msg 515, Level 16, State 2, Line 1
Cannot insert the value NULL into column 'contract', table 'pubs.dbo.authors'; column does not allow nulls. INSERT fails.
The statement has been terminated.

does anyone know how to capture the text of the error into a variable so I can insert it into a table?

SQL is useful if you don't know cursors :-)

Glockenspiel
Yak Posting Veteran

50 Posts

Posted - 2002-06-27 : 17:01:10
You mean something like this?

begin
INSERT authors (au_id) values ('172-32-1176')
SELECT description FROM master..sysmessages WHERE error = @@ERROR
end

Go to Top of Page

VyasKN
SQL Server MVP & SQLTeam MVY

313 Posts

Posted - 2002-06-27 : 17:10:05
You can not access the actual error message from within T-SQL. I discussed this at:
http://vyaskn.tripod.com/programming_faq.htm#q7
http://vyaskn.tripod.com/programming_faq.htm#q11

--
HTH,
Vyas
http://vyaskn.tripod.com
Go to Top of Page

lfmn
Posting Yak Master

141 Posts

Posted - 2002-06-28 : 07:59:47
If you run this code, you get the following results:

Cannot insert the value NULL into column '%.*ls', table '%.*ls'; column does not allow nulls. %ls fails.

which doesn't give you the table or column or which operation failed.

quote:

You mean something like this?

begin
INSERT authors (au_id) values ('172-32-1176')
SELECT description FROM master..sysmessages WHERE error = @@ERROR
end





SQL is useful if you don't know cursors :-)
Go to Top of Page

setbasedisthetruepath
Used SQL Salesman

992 Posts

Posted - 2002-06-28 : 08:44:03
This question has been posed before, I believe nr has a good solution.

Jonathan Boott, MCDBA
Go to Top of Page
   

- Advertisement -