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

Creating and updating table using function parameter reference

From: "Linux Guru" <linux(dot)binary(at)gmail(dot)com>
To: pgsql-performance(at)postgresql(dot)org
Subject: Creating and updating table using function parameter reference
Date: 2008-02-13 14:25:02
Message-ID: 3caa866c0802130625k728a4a5dl23bffb56c1ded6e9@mail.gmail.com (view raw or flat)
Thread:
Lists: pgsql-performance
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?

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

Responses

pgsql-performance by date

Next:From: Magnus HaganderDate: 2008-02-13 14:32:08
Subject: Re: Small DB Server Advice
Previous:From: MatthewDate: 2008-02-13 13:53:37
Subject: Re: Small DB Server Advice

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