SQL CHECK constraint issues
I'm using SQL Server 20开发者_开发技巧08 and I have a table with three columns: Length
, StartTime
and EndTime
. I want to make a CHECK constraint on this table which says that:
if Length == NULL then
StartTime <> NULL and EndTime <> NULL
else
StartTime == NULL and EndTime == NULL
I've begun to try things like this:
Length == NULL AND StartTime <> NULL AND EndTime <> NULL
Obviously this is not enough, but even this simple expression will not validate. I get the error:
"Error validating 'CK_Test_Length_Or_Time'. Do you want to edit the constraint?"
Any ideas on how to go about doing this?
CHECK ([Length] IS NULL AND [StartTime] IS NOT NULL AND [EndTime] IS NOT NULL
OR [Length] IS NOT NULL AND [StartTime] IS NULL AND [EndTime] IS NULL))
There is no == operator in SQL Server. While checking for null you have to use "is"
Please try this:
((Length is null AND starttime is not null AND endtime is not null) OR
(Length is not null AND starttime is null AND endtime is null))
HTH
精彩评论