Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2003
    Location
    Romania
    Posts
    70

    Unanswered: Update + Case + When

    I have 1 table (Test1)

    A B C D
    -----------------------------
    010 15.2 20 0
    030 13.8 25 0
    090 10.3 1.2 0
    010 7.25 10 0
    030 25.1 25.23 0
    010 21.01 14 0
    090 10.50 8 0

    I want to use SP to do something like this

    UPDATE Test1

    If [A] = '010' then
    SET [D] = [B]
    end if

    If [A] = '090' then
    SET [D] = [C]
    end if

    It is possible to use SP and only one UPDATE statement ?

    This is not acceptable (the table is read twice, 200.000 - 300.000 rows)

    UPDATE Test1
    set [D] = [B]
    WHERE [A] ='010'

    +

    UPDATE Test1
    set [D] = [C]
    WHERE [A] ='090'

  2. #2
    Join Date
    Nov 2002
    Location
    Jersey
    Posts
    10,322
    How about...
    Code:
    UPDATE Test1
       SET [D] = CASE WHEN [A] = '010' THEN [B]
                                WHEN [A] = '090' THEN [C]
                       END
    WHERE [A] IN ('010','090')
    Brett
    8-)

    It's a Great Day for America everybody!

    dbforums Yak CorralRadio 'Rita
    dbForums Member List
    I'm Good Once as I ever was

    The physical order of data in a database has no meaning.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •