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

    Thanks

  • #2
    Hi Iceberg,

    I don't know if I understand your question, do you need something like this?

    Code:
    Update Ppp
    Set Field3 =
    Case
    When Field1 = 'A'
    And Field2 = 'B'
    And Field3 = ' ' Then 'I'
    When Field1 = 'C'
    And Field2 = 'D'
    And Field3 = ' ' Then 'O'
    Else Field3
    End;

    Comment


    • #3
      Paolinosal's example sounds right -- except that it updates every single row in the table, which may not be optimal.

      To fix that, you could just add a 'where' cause to it, for example (reformatted a bit):

      Code:
      Update Ppp
      Set Field3 = Case
                   When Field1 = 'A' And Field2 = 'B' And Field3 = ' ' Then 'I'
                   When Field1 = 'C' And Field2 = 'D' And Field3 = ' ' Then 'O'
                   Else Field3
                   End
      where (Field1='A' and Field2='B' and Field3=' ')
         or (Field1='C' and Field2='D' and Field3=' ');

      Comment

      Working...
      X