Skip site navigation (1) Skip section navigation (2)

PL/SQL dynamic update error

From: "Eduardo Ferreira" <eduardo(dot)edusantos(at)gmail(dot)com>
To: pgsql-admin(at)postgresql(dot)org
Subject: PL/SQL dynamic update error
Date: 2007-07-25 18:46:05
Message-ID: 266b12630707251146q8c96d4an125d6835f0a236d0@mail.gmail.com (view raw or flat)
Thread:
Lists: pgsql-admin
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?

pgsql-admin by date

Next:From: Steve HoldowayDate: 2007-07-25 20:53:25
Subject: Re: Using Explain
Previous:From: Tom LaneDate: 2007-07-25 17:34:12
Subject: Re: Localisation

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group