PL/PGSQL - dynamic variable names

From: Ben Carbery <ben(dot)carbery(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: PL/PGSQL - dynamic variable names
Date: 2011-06-29 00:29:05
Message-ID: BANLkTik2X1u_2mXofk_sRghbXNsEa4by1A@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi,

I am trying to EXECUTE .. INTO a variable that I want to be dynamically
named.

stuff := '{a,b,c,d}';

FOR i IN 1..4 LOOP
thing := stuff[i];

-- stuff_a, stuff_b etc are functions - substitution works here
exec_string := 'SELECT stuff_' || thing || '(''' || arg1 || ''','''
|| arg2 || ''');';

-- thing not substituted
-- value of thing above is replaced on each iteration rather than
assigning result into variables a,b,c,d
EXECUTE exec_string INTO thing;

END LOOP;

The variable names always seems to be interpreted literally though. Is there
a way to make this work in PL/PGSQL?

Of course it would be nice if EXECUTE supported 'SELECT INTO' :)

cheers,

Ben

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Rob Sargent 2011-06-29 00:43:53 Re: PL/PGSQL - dynamic variable names
Previous Message Rich Shepard 2011-06-28 23:18:42 Re: DROP TABLE Appears to Fail [SOLVED]