Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2003
    Posts
    2

    Unanswered: checking dates with triggers or constraints

    Hi,

    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
    ON Prodn_Problem
    FOR INSERT,UPDATE
    AS
    DECLARE @start_date datetime
    SELECT @start_date = (SELECT startdate FROM inserted)
    DECLARE @end_date datetime
    SELECT @end_date = (SELECT enddate FROM inserted)
    BEGIN
    IF(@end_date < @start_date)
    ROLLBACK
    END

    Thanks.

  2. #2
    Join Date
    Dec 2002
    Location
    Antwerp, Belgium
    Posts
    227
    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.
    Johan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •