View Single Post

   
  #2 (permalink)  
Old 02-28-2008, 05:39 PM
David Portas
 
Posts: n/a
Default Re: Date/Time overlaps - urgent.

First, add a constraint, if you haven't already, to ensure that the "from"
datetime is less than the "to" datetime (I've guessed your primary key and
only included the essential columns)

CREATE TABLE CollateralBooking (cbid INTEGER UNIQUE, cbcpartnumber INTEGER,
cbddatetimefrom DATETIME NOT NULL, cbddatetimeto DATETIME NOT NULL, CHECK
(cbddatetimefrom<cbddatetimeto), PRIMARY KEY (cbcpartnumber,
cbddatetimefrom))

IF EXISTS
(SELECT *
FROM CollateralBooking AS A
JOIN CollateralBooking AS B
ON A.cbcpartnumber=B.cbcpartnumber AND A.cbid<>B.cbid
AND NOT (A.cbddatetimefrom > B.cbddatetimeto
OR A.cbddatetimeto < B.cbddatetimefrom))
/* Raise an error */

--
David Portas
------------
Please reply only to the newsgroup
--


Reply With Quote