update and "if...else" clause?

  • I have 2 sql statements:

    update t5

    set updatefield = 0

    from t5 inner join t6 on (t5.c1 = t6.d1)

    and ( (t5.c2 =t6.c2) and (t5.c3 = t6.d3))

    update t5

    set updatefield = 1

    from t5 inner join t6 on (t5.c1 = t6.d1)

    and ( (t5.c2 <> t6.c2) or (t5.c3 <> t6.d3))

    The inner join in the first update works on a certain set of values and the inner join in the second update works on the data missed by the first join.

    Is there any way I can combine these two ststaments into one Join, one update?

    TIA,

    barkingdog

  • UPDATE t5

    SET UpdateField = CASE

    WHEN t5.c2 = t6.c2 AND t5.c3 = t6.d3

    THEN 0

    ELSE 1

    END

    FROM t5

    INNER JOIN t6

    ON t5.c1 = t6.d1

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Thank You Jeff.

  • You bet... thank you for the feedback.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

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

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