I have one table with a bunch of fields. For simplicity, assume 5 fields:
unique_id, currency_name, stock_description, company_name, city_name
say, the sample values are:
INTQL123, USD, "INT Stock", "International Stock Corporation", "CHICAGO"
And another one called override_table that can contain the override values for any column:
unique_id, override_column, override_value
Sample values:
INTQL123, "city_name", "NEW YORK"
So it is overriding the value of the city "CHICAGO" with the new value "NEW YORK"
I want to be able to write an SQL UPDATE statement with such dynamic column names. We can assume they will only be of the varchar type. Any idea?
Yoy can achieve this with an anonymous PL/pgSQL block and dynamic SQL.
The code above is SQLi prone. Make sure that no malicious values exist in table
override_tablebefore running it.Edit
"is there a way to return the number of updates back to the caller?"
Anonymous
doblocks do not return anything. Two approaches come to my mind.dyn_update, and issueselect dyn_update()from Java;select count(*) from override_tableas it would return exactly the number of updates (unless the do block bangs an exception)