ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Update multiple row in SQL

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Update multiple row in SQL

    Hi All can anyone help with the below scenario that i need to update...
    FileA
    Field1 Field2 Field3
    A B I
    A B *Blank
    A B *Blank
    C D O
    C D *Blanks
    C D *Blanks
    C D *Blanks
    C D *Blanks
    So I need to update all A /B with value I which are *blank and C/D with O which are *blank. So any DB2 query to do the same

    Thanks

  • #2
    How about this:
    Code:
    update File
    set Field3 = case when Field1 = 'A' and Field2 = 'B' then 'I'
                      when Field1 = 'C' and Field2 = 'D' then 'O'
                      else ' ' end
    where Field 3 = ' '

    Comment


    • Brian Rusch
      Brian Rusch commented
      Editing a comment
      * where Field3 = ' '

  • #3
    Hi Brian Thanks for the response, Actually Consider field1 and field2 as composite key, so there can be so many keys like A/B and C/D, So i cant make it hardcode like this.

    Comment


    • #4
      want to update blank with I if Field1/Filed2 is A/B.

      Comment


      • #5
        I mean a generic query

        Comment


        • #6
          Rite now am using below query but not correct:-
          Update FileA as A set A.Field3=(Select B.Field3 from FileA as B where A.Field1=B.Field1 and A.Field2=B.Field2 and A.Field3<>' ' and B.Field3<>' ') where
          Exists (Select 1 from FileA as B where A.Field1=B.Field1 and A.Field2=B.Field2 and A.Field3<>' ' and B.Field3<>' ')

          But this query only updating rows where Field3 not *Blank.

          Comment


          • #7
            IMHO you have to change
            Code:
            A.Field3<>' ' and B.Field3<>' '
            to
            Code:
            A.Field3 = ' ' and B.Field3 <> ' '
            Birgitta

            Comment

            Working...
            X