I want to add a check constraint to an already-existing table which does a check based upon a SELECT COUNT(*) statement. I know my SELECT statement but when I have created the DROP AND CREATE TO script I am unsure where to add my code and what format it should take?
Is there a way to do this in the GUI, or could someone show me an example of a CREATE table statement with a CHECK constraint, considering of a SELECT COUNT(*)?
I know that a CHECK constraint just has to evaluate to a boolean from here: http://msdn.microsoft.com/en-gb/library/ms188258(v=sql.105).aspx
The first thing of note is that you cannot directly use sub-queries in check constraints, but creating a function is the way around that. This will help you: