Create custom error message in check constraints in SQL SERVER 2008

oscar.fimbres picture oscar.fimbres · Nov 30, 2011 · Viewed 15.3k times · Source

I'd like to see the ability to attach custom error messages to CONSTRAINT objects, specifically CHECK constrints. Either directly or via a custom error number in sysmessages.

I've seen developers have to create triggers. I think that's not a good reason to implementing it.

I'm using SQL SERVER 2008.

Answer

Luke picture Luke · Oct 16, 2012

You could name your constraint with a user message.

For Example:

ADD CONSTRAINT 
[Foo cannot be greater than Bar. Please be sure to check your foos and bars next time.] 
CHECK (foo <= Bar)