Search code examples
sql-serverdatabasesql-server-2008check-constraints

Create custom error message in check constraints in SQL SERVER 2008


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.


Solution

  • 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)