Need help with conditional update

  • I realize that this syntax is wrong, but it describes what I would like to achieve.

    UPDATE dbo.tblUsers

    SET GroupId = '98'

    WHERE UsrSName = 'shortname'

    AND GroupID = '7'

    OR SET GroupID = '99’

    WHERE UsrSName = 'shortname'

    AND GroupID <> '7'

    I appreciate any help given.

  • UPDATE dbo.tblUsers

    SET GroupId = CASE GroupID WHEN '7' THEN '98' ELSE '99' END

    WHERE UsrSName = 'shortname'

    John Rowan

    ======================================================
    ======================================================
    Forum Etiquette: How to post data/code on a forum to get the best help[/url] - by Jeff Moden

  • Use CASE instead of OR

    UPDATE dbo.tblUsers SET

    GroupId =

    CASE

    WHEN GroupID = '7' THEN '98'

    ELSE '99'

    END

    WHERE UsrSName = 'shortname'

    Greets

    Flo

  • Thanks, John.

  • Thanks, Flo.

  • Heh... proof positive that great minds think alike. 😉 Look at the times on those two posts...

    --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 6 posts - 1 through 5 (of 5 total)

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