From: | "Patrick Fiche" <patrick(dot)fiche(at)aqsacom(dot)com> |
---|---|
To: | "'Johann Uhrmann'" <johann(dot)uhrmann(at)xpecto(dot)com> |
Cc: | "PostgreSQL - General (E-mail)" <pgsql-general(at)postgresql(dot)org> |
Subject: | Re: Getting results from a dynamic query in PL/pgSQL |
Date: | 2003-01-29 11:07:28 |
Message-ID: | 85058ADF852DD5118FD50002A528A5B6079B52@SERVEUR |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
I'm not sure I really understood what you want...
Here is perhaps what you need...
CREATE TABLE t1
(
col1 text
);
create or replace function foo(text) returns text as '
DECLARE
colname ALIAS FOR $1;
sqlquery text;
result text;
BEGIN
sqlquery := ''INSERT INTO t1 (col1 ) SELECT '' || colname || '' from
toto'';
EXECUTE( sqlquery );
SELECT INTO result col1 FROM t1;
DELETE FROM t1;
RETURN result;
END;
' language 'plpgsql';
----------------------------------------------------------------------------
---------------
Patrick Fiche
email : patrick(dot)fiche(at)aqsacom(dot)com
tél : 01 69 29 36 18
----------------------------------------------------------------------------
---------------
-----Original Message-----
From: pgsql-general-owner(at)postgresql(dot)org
[mailto:pgsql-general-owner(at)postgresql(dot)org]On Behalf Of Johann Uhrmann
Sent: Monday, January 27, 2003 4:02 PM
To: pgsql-general(at)postgresql(dot)org
Subject: [GENERAL] Getting results from a dynamic query in PL/pgSQL
Hello,
is there a possibility to retrieve results from a dynamically generated
query in PL/pgSQL?
That would be like this:
create function foo(text) returns text as '
DECLARE
colname ALIAS FOR $1;
result text;
BEGIN
SELECT INTO result colname from my_table;
RETURN result;
END;
' language 'plpgsql';
The actual column that is to be read from my_table should be passed as
argument to the function.
The example does not read the variable colname but tries to read a
column named "colname" from my_table which is not how it should work.
The keyword "EXECUTE" which could execute dynamic queries cannot return any
value that a select statement would. (At least there is nothing about that
in the docs.)
In order to write a trigger function that reads column names out of a table
and uses those column names afterwards, I need such a functionality.
I tried a workaround by using EXECUTE to create a function that reads
only the columns I need and then calling this dynamically generated
function.
However, that works only one time - then I get the following message:
ERROR: plpgsql: cache lookup for proc 52118 failed
plpgsql seems to cache the dynamically generated function even after it gets
dropped or overwritten with "create or replace function...".
Does anyone know how to use column names stored in variables within
PL/pgSQL?
Thank You in advance,
Johann Uhrmann
---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo(at)postgresql(dot)org so that your
message can get through to the mailing list cleanly
From | Date | Subject | |
---|---|---|---|
Next Message | pginfo | 2003-01-29 11:11:44 | Rules for trigger execution |
Previous Message | Shridhar Daithankar<shridhar_daithankar@persistent.co.in> | 2003-01-29 10:48:24 | Re: Using RSYNC for replication? |