Skip to content
Advertisement

How to pick only a particular set of values from a column and generate an update statement based on that

I have a problem where I need to run a set of update statements based on column values.

So we will get a set of IDs like below after a select query.

Where ChangedColumns are the column names which are changed and ChangedValues will tell the old and new values for the respective columns (delimited by a pipe ‘|’ and enclosed in braces ‘[]’)

Based on this, I need to run update statements on another table, which will be something like

The number of Changed Columns can be 1 – 20 and its corresponding changed values will be in same order in ChangedValues column.

Could someone please let me know how to write a PL/SQL block for the same?

Many thanks

Advertisement

Answer

First I broke the data down by ID and ChangedColumn:

Then changed that to generate a separate update statement for each column that changed for each ID. The next step is to work it into one update statement for each ID, handling all columns that changed:

EDIT: Here’s the final product after some further refining and tweaking:

I suspect this could be further simplified, but there’s something to be said for breaking a problem down into smaller steps.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement