Recursive Update Trigger

  • I am trying to create an update trigger that updates isactive field depending on different criteria.

    If DateField1 is >= GETDATE() OR DateField2 >= GETDATE() AND Extension =1 then IsActive is true.

    If DateField1 is < GETDATE() AND DateField2 < GETDATE() OR DateField2 IS NULL then IsActive is false.

    Right now I have this as two separate updates, but that makes the trigger recursive. Can I combine these using CASE statement?

  • Erin-489205 (3/13/2013)


    I am trying to create an update trigger that updates isactive field depending on different criteria.

    If DateField1 is >= GETDATE() OR DateField2 >= GETDATE() AND Extension =1 then IsActive is true.

    If DateField1 is < GETDATE() AND DateField2 < GETDATE() OR DateField2 IS NULL then IsActive is false.

    Right now I have this as two separate updates, but that makes the trigger recursive. Can I combine these using CASE statement?

    Talk about sparse on details....

    This might be close but without any more details it is hard to say.

    update table

    set IsActive

    case

    when DateField1 is >= GETDATE() OR DateField2 >= GETDATE() AND Extension =1 then 'true'

    when DateField1 is < GETDATE() AND DateField2 < GETDATE() OR DateField2 IS NULL then 'false'

    else NULL

    end

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Sean Lange (3/13/2013)


    Erin-489205 (3/13/2013)


    I am trying to create an update trigger that updates isactive field depending on different criteria.

    If DateField1 is >= GETDATE() OR DateField2 >= GETDATE() AND Extension =1 then IsActive is true.

    If DateField1 is < GETDATE() AND DateField2 < GETDATE() OR DateField2 IS NULL then IsActive is false.

    Right now I have this as two separate updates, but that makes the trigger recursive. Can I combine these using CASE statement?

    Talk about sparse on details....

    This might be close but without any more details it is hard to say.

    update table

    set IsActive

    case

    when DateField1 is >= GETDATE() OR DateField2 >= GETDATE() AND Extension =1 then 'true'

    when DateField1 is < GETDATE() AND DateField2 < GETDATE() OR DateField2 IS NULL then 'false'

    else NULL

    end

    If this is in a trigger, shouldn't the solution be something like this (to avoid recursion)?

    update INSERTED

    set IsActive

    case

    when DateField1 is >= GETDATE() OR DateField2 >= GETDATE() AND Extension =1 then 'true'

    when DateField1 is < GETDATE() AND DateField2 < GETDATE() OR DateField2 IS NULL then 'false'

    else NULL

    end


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

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

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