Need a better way to write the query

  • Hi all,

    I am trying to perform updates in a loop rather than writing mutiple update statements.

    The where clause changes.

    I am using a WHILE LOOP, with @var.

    The first time, i.e CASE WHEN @i = 0, THEN T1.var1 = T2.var1

    and during the second time, WHEN @i = 1, THEN T1.Var2 = T2.Var2.

    Is there anyway to write the above ?

    Thanks in advance!

  • Offer suggestions we can't, your code we can't from here see. The force strong enough it isn't.

    Please post your code, we really can't help you if we don't know wht you are doing.

    Also, you may want to take a few minutes and read the first article I have referenced below in my signature block regarding asking for help.

  • The following is the piece of code :).

    WHILE (@i < 2)

    BEGIN

    SELECT *

    FROM Table1 T1

    JOIN Table2 T2

    ON T1.var1 = T2.var1

    AND

    CASE @i

    WHEN 0 THEN T1.var2 = T2.var2

    WHEN 1 THEN T1.var3 = T2.var3

    END

    END

  • "Case" doesn't work that way in T-SQL. Try this:

    SELECT *

    FROM Table1 T1

    JOIN Table2 T2

    ON T1.var1 = T2.var1

    AND

    (@i = 0 AND T1.var2 = T2.var2

    or

    @i = 1 AND T1.var3 = T2.var3)

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • Thanks a ton!!!

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

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