I know this is an old post, but I've found something that may make it a bit easier to provide clearer error messages for check constraints to the end-user: the names of check constraints can include carriage returns and line feeds, so the error message can be made a bit easier to see.
E.g. creating the following constraint produces the error message below. (the blank lines between the [
and ]
are intentional i.e. they are part of the constraint name.)
ALTER TABLE dbo.Sales WITH CHECK ADD CONSTRAINT [
ERROR:
You have stupidly entered a negative selling price. Please report to detention.
] CHECK ([SellingPrice] >= 0.00)
GO
And when this constraint fails, the resulting message is:
I tried putting markup in the error message (i.e. constraint name), like <b>message</b>
and *message*
, but to no avail. And it may be possible, but really unwieldy, to use this for foreign key constraints as well. I haven't tried it.
So it's not a 100% solution, but hopefully easier for the user to see the intended error message.
Edit (2022-02-09): Since database object names are stored using the sysname data type (search for 'sysname' on this page), they cannot be longer than 128 characters. Use short error messages π