I have the following table:
CREATE TABLE dbo.myTable]
(
ID1 [int] NOT NULL,
ID2 [int] NOT NULL,
StartDate smalldatetime NOT NULL,
EndDate smalldatetime NULL,
CONSTRAINT [PK_myTable1] PRIMARY KEY CLUSTERED (ID1 ASC, ID2 ASC, StartDate ASC)
) ON [PRIMARY]
I want to ensure that the StartDate
to EndDate
period for each ID1 and ID2 is unique, and that there is no overlap.
How to create a check constraint like this:
(
ID1 <> existingRow.ID1
or ID2 <> existingRow.ID2
)
or (
ID1 = existingRow.ID1
and ID2 = existingRow.ID2
and (
StartDate >= isnull(existingRow.EndDate, Startdate + 1)
or isnull(EndDate, existingRow.StartDate + 1) <= existingRow.StartDate
)
)
...or a constraint with a condition like this:
IF ID1 = existingRow.ID1 and ID2 = existingRow.ID2
CHECK (
StartDate >= isnull(existingRow.EndDate, Startdate + 1)
or isnull(EndDate, existingRow.StartDate + 1) <= existingRow.StartDate
)
Thanks in advance...