Connecting Tech Pros Worldwide Help | Site Map

Getting check constraint error from sproc?

zachster17's Avatar
Newbie
 
Join Date: Dec 2007
Location: Indiana
Posts: 30
#1: Jul 19 '08
Hello everybody,

I was wondering if it was possible to get the name of the check constraint that caused an error (if it was the cause of an error) in a stored procedure as a way to handle errors.

For example, I have a stored procedure that adds a record to a table and I'd like to have a more user-friendly error message than just showing the error name, which still is useful.

Thank you,

Zach
ck9663's Avatar
Expert
 
Join Date: Jun 2007
Posts: 1,925
#2: Jul 19 '08

re: Getting check constraint error from sproc?


Check out Try...Catch

Here are also some good reference.

-- CK
zachster17's Avatar
Newbie
 
Join Date: Dec 2007
Location: Indiana
Posts: 30
#3: Jul 20 '08

re: Getting check constraint error from sproc?


Quote:

Originally Posted by ck9663

Check out Try...Catch

Here are also some good reference.

-- CK

I checked out the TRY/CATCH but I'm stuck with SQL Server 2000 for this job, so I can't use this. Is there anyway to capture the name of a check constraint using the 2000 method (of checking @@ERROR) before the constraint actually stops the stored procedure (without doing a trigger of sorts?)
Delerna's Avatar
Expert
 
Join Date: Jan 2008
Location: Sydney
Posts: 787
#4: Jul 21 '08

re: Getting check constraint error from sproc?


You could always do your error checking in the stored proc itself and return your own sensible error messages back to the front end.
zachster17's Avatar
Newbie
 
Join Date: Dec 2007
Location: Indiana
Posts: 30
#5: Jul 21 '08

re: Getting check constraint error from sproc?


Quote:

Originally Posted by Delerna

You could always do your error checking in the stored proc itself and return your own sensible error messages back to the front end.


Thanks Delerna--that seems like the only way. I'm kind of inexperienced when it comes to working in companies with SQL Server, but is it standard to do a lot of validating/error checking in stored procedures (instead of enforcing rules through table constraints and keys, etc.)

Zach
Delerna's Avatar
Expert
 
Join Date: Jan 2008
Location: Sydney
Posts: 787
#6: Jul 21 '08

re: Getting check constraint error from sproc?


No, not instead of. Setting up table constraints,keys and relationships is very important. But as you mention in your post, left to its own devices, SQL server dosn't supply the friendliest of error messages back to a front end. So what I was refering to is more in "addition to" so that you as the developer can supply friendlier error messages (in situations where that is important) to your users.
Reply