Message returned when a check constraint violated

  • I have several check constraints in place. E.g. len (Field1) = 6

    Is there a way of specifying a user-defined message when a particular constraint is violated? So for each constraint, there will be a unique message that relates to the violation. Can any thing other than triggers achieve this?

    NB I do not want to change the message in sysmessages.

  • Form the section "Triggers Compared to Constraints" in BOL Article "Enforcing Business Rules with Triggers"

    quote:


    Constraints can communicate about errors only through standardized system error messages. If your application requires (or can benefit from) customized messages and more complex error handling, you must use a trigger.


  • You can put a short message inside the name of CK. When a user reads a violation message, he will see that message too. I do it like this:

    CK_Table_1_[_Field_length_must_be_6_]

    -where CK= Check Constraint, Table = table name, 1 = the number of CK on this table, if you have several, you only need the user to tell you the number, not the whole message, [] = message inside paretheses.

    Try it, if a user carefully looks at the error message he will see your note.

    That's as far as it goes without triggers.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply