SQL CHECK Constraint Issues

I am using SQL Server 2008, and I have a table with three columns: Length, StartTimeand EndTime. I want to make a CHECK constraint for this table, which says that:

if Length == NULL then
  StartTime <> NULL and EndTime <> NULL
else
  StartTime == NULL and EndTime == NULL

I started trying things like this:

Length == NULL AND StartTime <> NULL AND EndTime <> NULL

Obviously, this is not enough, but even this simple expression will not be checked. I get an error message:

"Verification error 'CK_Test_Length_Or_Time'. Do you want to edit the restriction?"

Any ideas on how to do this?

+3
source share
2 answers

SQL Server ==. "is"

, :

((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))

+3
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))
+4

Source: https://habr.com/ru/post/1741436/


All Articles