sqloracle-databasepeoplesoft

concat multiple strings of a column in different update statements


I need to update the column Remark that depends on the column in different update statements if it has an update and my code below doesn't work

It also needs to be separated by a comma (,)

UPDATE %Table(staging_tbl) S
SET S.REMARK= %Concat(S.REMARK || ',email update'), A.EMAIL = (SELECT A.EMAIL .....)


UPDATE %Table(staging_tbl) S
SET S.REMARK= %Concat(S.REMARK || ',age update'), A.AGE = (SELECT A.AGE.....)

UPDATE %Table(staging_tbl) S
SET S.REMARK= %Concat(S.REMARK || ',name update'), A.NAME = (SELECT A.NAME.....)

What can I add if the string to be added depends on if there's an update in that column?


Solution

  • In Oracle, use a single statement and concatenate the strings using a CASE expression to determine whether there were any changes:

    UPDATE staging_tbl
    SET age    = :age,
        email  = :email,
        name   = :name,
        remark = remark
                 || CASE WHEN :age   <> age   THEN ',age updated'   END
                 || CASE WHEN :email <> email THEN ',email updated' END
                 || CASE WHEN :name  <> name  THEN ',name updated'  END
    WHERE id = :id
    

    db<>fiddle here