Results 1 to 6 of 6

Thread: DB2 Update SQL

  1. #1
    Join Date
    Apr 2005
    Location
    USA
    Posts
    130

    Unanswered: DB2 Update SQL

    Hi,
    I need to update a table in a column with data from another column in same table under certain conditions.

    Before Update
    HTML Code:
    COL1    COL2    COL3
     11        1         101
     12        2         101
     13        4         101
     14        5         101
    After Update
    HTML Code:
    COL1    COL2    COL3
     13        1         101
     14        1         101
     13        2         101
     14        2         101
    Is some thin can be done through SQL. I have thousands of records to update. My update SQL doesn't work. Any ideas?

    UPDATE TABLE
    SET COL1 = (SELECT COL1 FROM TABLE
    WHERE COL3 = 101
    AND COL2 = 13)

  2. #2
    Join Date
    Jan 2003
    Posts
    4,292
    Provided Answers: 5
    There is not enough information in your question. Try explaining what you want to do in detail.

    Andy

  3. #3
    Join Date
    Jun 2003
    Location
    Toronto, Canada
    Posts
    5,516
    Provided Answers: 1
    Quote Originally Posted by schintala View Post
    My update SQL doesn't work.
    Is your computer turned on?
    ---
    "It does not work" is not a valid problem statement.

  4. #4
    Join Date
    Nov 2011
    Posts
    334
    plz explain your requirement more specificly.
    I can not understand what the logic is through your sample data.

  5. #5
    Join Date
    Apr 2005
    Location
    USA
    Posts
    130
    Thank you so much for the reply. I am sorry for the confusion.
    In a group of COL3 say 101, If there is 4,5 under Col2, then take value of COL1(13, 14) for those and update the COL1 for the 1,2.

    Then Update COL2 from 1,2 to 1, 1 and 4,5 to 2,2.

  6. #6
    Join Date
    Oct 2012
    Posts
    11
    It seems to me you want to use a CASE statement something like:

    Set Col1 = Case
    when Col3 = XX and Col2 = YY then <value>
    when Col3 = XY and Col2 = XY then <value>
    else Col1

    That might work for you.

Posting Permissions

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