dbTalk Databases Forums  

pl/pgsql Function Problem

comp.databases.postgresql.novice comp.databases.postgresql.novice


Discuss pl/pgsql Function Problem in the comp.databases.postgresql.novice forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Reinhard Hnat
 
Posts: n/a

Default pl/pgsql Function Problem - 01-28-2004 , 06:42 AM






I have written a pl/pgsql function as follows:

create function aufloesung(varchar,varchar) returns integer as '
declare
tabnam alias for $1;
proj alias for $2;
begin
drop table temptab;
create table $1 as select sum(menge) as menge, bauteilcode, version from
fertigungsprojektliste where fertigungsprojekt=''proj'' group by
bauteilcode,version;
return 1;
end;'
language 'plpgsql';

When I call this function with:
select aufloesung('xyz','0307W2901');
an Error Message is issued: ERROR: 'parserarse error at or near "$1" at
character 15'

When I change "create table $1" to "create table xyz" the function works
fine. What did I do wrong?

Thanks in advance for your hints.
R.Hnat


---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend


Reply With Quote
  #2  
Old   
Stephan Szabo
 
Posts: n/a

Default Re: pl/pgsql Function Problem - 01-28-2004 , 10:23 AM







On Wed, 28 Jan 2004, Reinhard Hnat wrote:

Quote:
I have written a pl/pgsql function as follows:

create function aufloesung(varchar,varchar) returns integer as '
declare
tabnam alias for $1;
proj alias for $2;
begin
drop table temptab;
create table $1 as select sum(menge) as menge, bauteilcode, version from
fertigungsprojektliste where fertigungsprojekt=''proj'' group by
bauteilcode,version;
return 1;
end;'
language 'plpgsql';

When I call this function with:
select aufloesung('xyz','0307W2901');
an Error Message is issued: ERROR: 'parserarse error at or near "$1" at
character 15'

When I change "create table $1" to "create table xyz" the function works
fine. What did I do wrong?
Commands like create table do not currently accept variables as arguments
in plpgsql. You'll need to use EXECUTE to do them, like:
EXECUTE ''create table '' || $1 || '' as ...


---------------------------(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



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.