Insert Trigger

  • Guys,

    I have 15 tables where the combination of CODE, range for EFFECTIVE DATE and END DATE columns should be unique.

    For example

    IDCDEFF DTEND DT

    ______________________________________

    1CO02/02/08 02/15/08

    2CO02/04/0902/29/08

    3CO02/16/0802/29/08

    In the above example the table should throw an error for 2nd row ID = 2 since the effective and end date overlap with

    1st row ID = 1

    Is there any way to accomplish this using triggers on the table?

    Any suggestions and inputs would help

    Thanks

  • If you are validating the CD column with the help of Eff Date and End Date, then in the INSERT trigger a query with "Where" clause can help you out.

    Pradeep Adiga
    Blog: sqldbadiaries.com
    Twitter: @pradeepadiga

  • I think this is simple if I understand what you are looking for.

    In the trigger, look for the inserted table and check if it's dates and code are inside an existing row.

    You could do something like

    create trigger ...

    if exists ( select top 1 cd

    from TableA a

    inserted b

    where b.eff betwen a.eff and a.end

    )

    rollback

    return

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply