Unanswered: checking dates with triggers or constraints
I have a table that contains various data with a start and end date. When a user enters the information I need to check that the end date is older than the start date.
What is the easiest way to do this - using a trigger?
And is the code below correct ?
CREATE TRIGGER checkdate
DECLARE @start_date datetime
SELECT @start_date = (SELECT startdate FROM inserted)
DECLARE @end_date datetime
SELECT @end_date = (SELECT enddate FROM inserted)
IF(@end_date < @start_date)
By using a trigger, you check the data when it's already inserted or updated in the table. When you use a check constraint you can verify before the information is entered. Another method is to create a stored procedure which does the checking an let you users enter information through this stored procedure.