I am trying to rename all columns in a table, which contain the string survey_ by simply removing this string from the column name. I wish to do this with dynamic SQL. I've formulated the following query, which runs without throwing an error, but nothing happens, vis-a-vis, no column names get renamed and I don't understand why. Can someone assist me:
do
$$
declare
l_rec record;
begin
for l_rec in (select table_schema, table_name, column_name
from information_schema.columns
where table_schema = 'LUCAS_LF2022'
and table_name = 'l2022_survey_lf_tst_pkg'
and column_name like 'survey%') loop
execute format ('alter table %I.%I rename column %I to REPLACE(%I, ''survey'', '''')', l_rec.table_schema, l_rec.table_name, l_rec.column_name, l_rec.column_name);
end loop;
end;
$$
You don't need to use the
REPLACEfunction directly on%I. Instead, you should construct the new column name in the format string itself.