Thread: PL/SQL dynamic update error
I have a table with informations that change every month, and I have to update the monthly data in a new column. To do that, I try to find out wich months are being trated and I perform an update in my table for every month.
So what I'm trying to do is to change the month column for a fixed name, perform my selects and updates in the renamed column and change the name back for its old value. Then I do exactly the same thing for another month. It would be something like this:
FOR month IN select month from monht_values LOOP
v_command: = 'ALTER TABLE my_table
RENAME COLUMN m'
||month
||' TO month_value;';
EXECUTE v_command;
UPDATE my_table
SET month_value = new_value
WHERE key = my_key;
v_command: = 'ALTER TABLE my_table
RENAME COLUMN month_value TO m'
||month
||';';
EXECUTE v_command;
END LOOP;
The query works just fine, but the updates are made allways in the same column. Even when I change the name, PostgreSQL doesn't recognize it, and all the updates are performed in the same first column name. It seems like it stores the column information in a way I can't change the it.
Any ideas on how to fix it?
So what I'm trying to do is to change the month column for a fixed name, perform my selects and updates in the renamed column and change the name back for its old value. Then I do exactly the same thing for another month. It would be something like this:
FOR month IN select month from monht_values LOOP
v_command: = 'ALTER TABLE my_table
RENAME COLUMN m'
||month
||' TO month_value;';
EXECUTE v_command;
UPDATE my_table
SET month_value = new_value
WHERE key = my_key;
v_command: = 'ALTER TABLE my_table
RENAME COLUMN month_value TO m'
||month
||';';
EXECUTE v_command;
END LOOP;
The query works just fine, but the updates are made allways in the same column. Even when I change the name, PostgreSQL doesn't recognize it, and all the updates are performed in the same first column name. It seems like it stores the column information in a way I can't change the it.
Any ideas on how to fix it?