I'm trying to enforce integrity on a table in a way which I do not think a Constraint can...
CREATE TABLE myData
(
id INTEGER IDENTITY(1,1) NOT NULL,
fk_item_id INTEGER NOT NULL,
valid_from DATETIME NOT NULL,
invlaid_from DATETIME NOT NULL
)
The constraint I want to apply is that there should never be any entries for the same "fk_item_id" with overlapping dates.
Note:
invalid_from is the instant immediately after the valid period.
This means that the following two periods are fine...
- '2008 Jan 01 00:00' -> '2008 Feb 01 00:00' (All of Jan)
- '2008 Feb 01 00:00' -> '2008 Mar 01 00:00' (All of Feb)
I can check that rule in a trigger. When the trigger does find an illegal insert/update, however, what is the best way to prevent the "illegal" inserts/updates from happening?
(If inserted includes two valid records and two invalid records, can I stop just the two invalid records?)
Cheers,
Dems.
EDIT:
In the case I had above, a constraint using a function worked well. But I never worked out why the RAISERROR didn't work in the trigger version.
I thought it was because the trigger is an AFTER trigger, and that I'd need a BEFORE trigger, but that doesn't appear to be an option...