July 10, 2010 at 9:15 am
Hi, I am new to SQL databases and have been having trouble doing the following. I need to check that the the maximum value in one column of one table does not exceed the maximum value of one column in another table using a CHECK constraints. How do I do that if it does not allow me to use the MAX() function? The error message complains about the use of an aggregate function.
Help!!!
July 10, 2010 at 9:32 am
Trigger?
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
July 10, 2010 at 9:38 am
I am able to do it fine with a trigger but I am required to do it with a CHECK.
July 10, 2010 at 12:11 pm
July 10, 2010 at 12:38 pm
How about something like:
select top (1) TheFieldImInterestedIn
from TheTableIHaveToPerformMagicWith
order by TheFieldImInterestedIn DESC
Check constraints can utilize a function, so you might need to embed your max-field-comparison-between-two-fields logic in that.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
July 10, 2010 at 1:34 pm
You would actually need two CHECK constraints because this is effectively a constraint on both tables whereas each CHECK constraint can apply only to one table.
July 10, 2010 at 1:45 pm
index_us (7/10/2010)
I am able to do it fine with a trigger but I am required to do it with a CHECK.
Why are you 'required' to use a constraint? Is this a homework type exercise?
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply