From: Linux Guru <linux.binary@gmail.com>
To: Albert Cervera Areny <albert@sedifa.com>
Cc: pgsql-performance@postgresql.org
Subject: Re: Creating and updating table using function parameter reference
Date: Thu, 14 Feb 2008 17:35:27 +0500
Message-ID: <3caa866c0802140435v4af4f2a1y183883db977eb151@mail.gmail.com> (raw)
In-Reply-To: <200802131623.57343.albert@sedifa.com>
References: <3caa866c0802130625k728a4a5dl23bffb56c1ded6e9@mail.gmail.com>
<200802131623.57343.albert@sedifa.com>
I still cannot pass tablename, what is wrong?
Is this the right way?
CREATE OR REPLACE FUNCTION test ( t1 text,t2 text ) RETURNS numeric AS $$
declare temp1 text;
declare temp2 text;
declare cmd text;
declare t2row RECORD;
begin
temp1=t1;
temp2=t2;
cmd='select product, (case when sum(pd) <> 0 then sum(gd)/sum(pd)*100
else 0 end ) as gppp
from ' temp1 ' as dummy group by dummy.product,dummy.totalclaimsgroup,
dummy.avgmems,dummy.months';
execute cmd into t2row
--After executing above, I need here to update table t1
end;
$$ LANGUAGE plpgsql
----------------
ERROR: syntax error at or near "$1"
LINE 2: from ' $1 ' as dummy group by dummy.product,dummy.totalcla...
^
QUERY: SELECT 'select product, (case when sum(pd) <> 0 then
sum(gd)/sum(pd)*100 else 0 end ) as gppp
from ' $1 ' as dummy group by dummy.product,dummy.totalclaimsgroup,
dummy.avgmems,dummy.months'
CONTEXT: SQL statement in PL/PgSQL function "test" near line 9
********** Error **********
ERROR: syntax error at or near "$1"
SQL state: 42601
Context: SQL statement in PL/PgSQL function "test" near line 9
On Wed, Feb 13, 2008 at 8:23 PM, Albert Cervera Areny <albert@sedifa.com>
wrote:
> A Dimecres 13 Febrer 2008 15:25, Linux Guru va escriure:
> > I want to create and update two tables in a function such as below, but
> > using parameters as tablename is not allowed and gives an error. Is
> there
> > any way I could achieve this?
>
> You're looking for EXECUTE:
>
> http://www.postgresql.org/docs/8.3/static/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN
>
> >
> > CREATE OR REPLACE FUNCTION test ( t1 text,t2 text ) RETURNS numeric AS
> $$
> > declare temp1 text;
> > declare temp2 text;
> > begin
> > temp1=t1;
> > temp2=t2;
> > select
> > product,
> > (case when sum(pd) <> 0 then sum(gd)/sum(pd)*100 else 0 end ) as gppp
> > into temp2 from temp1 as dummy
> > group by dummy.product,dummy.totalclaimsgroup,dummy.avgmems,dummy.months
> ;
> >
> > update temp1 as t set
> > GPPP=(select gppp from temp2 as dummy where dummy.product=t.product),
> >
> > end
> > $$ LANGUAGE plpgsql
> >
> >
> > ----------------------
> > ERROR: syntax error at or near "$1"
> > LINE 1: ...en sum(gd)/sum(pd)*100 else 0 end ) as gppp from $1 as
> dum...
> > ^
> > QUERY: select product, (case when sum(pd) <> 0 then sum(gd)/sum(pd)*100
> > else 0 end ) as gppp from $1 as dummy group by dummy.product,
> > dummy.totalclaimsgroup,dummy.avgmems,dummy.months
> > CONTEXT: SQL statement in PL/PgSQL function "test" near line 10
> >
> > ********** Error **********
> >
> > ERROR: syntax error at or near "$1"
> > SQL state: 42601
> > Context: SQL statement in PL/PgSQL function "test" near line 10
>
>
>
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-performance@postgresql.org
Cc: linux.binary@gmail.com, albert@sedifa.com
Subject: Re: Creating and updating table using function parameter reference
In-Reply-To: <3caa866c0802140435v4af4f2a1y183883db977eb151@mail.gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox