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
 Transact-SQL (2000)
 Critical error trapping

Author  Topic 

ourspt
Starting Member

33 Posts

Posted - 2005-03-24 : 01:43:14
Hi,

I have a situation where I need to give a custom error message in an SP where I get a primary key violation. I tried this with the @@error variable as in the following code.

CREATE PROCEDURE TESTspt1PROC
AS
insert into testspt1 values (1, 'spt')
if @@error <> 0
PRINT 'it is a Primary Key error and ..'

There is already a record in testspt1 table with the value '1'. When I execute the procedure, I get the following output:


Violation of PRIMARY KEY constraint 'PK__TestRaj1__7D2E8C24'. Cannot insert duplicate key in object 'TestRaj1'.
The statement has been terminated.
'it is a Primary Key error and ..'

i.e, it is giving the SQL Server standard message first and then giving the custom message I gave. I need to get only the custom message and not the SQL Server standard message in addtion.

Is there any way to make it possible?

Thanks in advance
ourspt

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2005-03-24 : 01:53:59
Only way I know of is to check for the primary key violation prior to doing the insert. But that'll give you performance hit.

Tara
Go to Top of Page
   

- Advertisement -