pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
table name as parameter in pl/psql
2+ messages / 2 participants
[nested] [flat]

* table name as parameter in pl/psql
@ 2001-12-10 08:39  darthxiong@libero.it <darthxiong@libero.it>
  0 siblings, 1 reply; 2+ messages in thread

From: darthxiong@libero.it @ 2001-12-10 08:39 UTC (permalink / raw)
  To: pgsql-admin


hi all, i'm trying to do something like this:
 
CREATE FUNCTION read_table(text) RETURNS int AS '
 DECLARE 
         table_name ALIAS FOR $1;
          res
  INTERGER;
 BEGIN
          SELECT INTO res COUNT(id) FROM table_name;
          RETURN res;
 END;
 ' LANGUAGE 'plpgsql';
 
using psql the creation return no errors, but the statement
 SELECT read_table( 'books' ) AS how_many;
 resuts in
 ERROR: parser: parse error at or near "$1"
 
and the same using 
SELECT INTO res COUNT(id) FROM $1;
 instead of 
SELECT INTO res COUNT(id) FROM table_name;
 while 
SELECT INTO res COUNT(id) FROM books ( the real name of the table )
 works good
 
there is a way to use a table name as parameter? where am i wrong?
 thx 
Ivan



^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: table name as parameter in pl/psql
@ 2001-12-10 21:54  Stephan Szabo <sszabo@megazone23.bigpanda.com>
  parent: darthxiong@libero.it <darthxiong@libero.it>
  0 siblings, 0 replies; 2+ messages in thread

From: Stephan Szabo @ 2001-12-10 21:54 UTC (permalink / raw)
  To: darthxiong@libero.it <darthxiong@libero.it>; +Cc: pgsql-admin


On Mon, 10 Dec 2001, [utf-8] darthxiong@libero.it wrote:

>
> hi all, i'm trying to do something like this:
>
> CREATE FUNCTION read_table(text) RETURNS int AS '
>  DECLARE
>          table_name ALIAS FOR $1;
>           res
>   INTERGER;
>  BEGIN
>           SELECT INTO res COUNT(id) FROM table_name;
>           RETURN res;
>  END;
>  ' LANGUAGE 'plpgsql';
>
> using psql the creation return no errors, but the statement
>  SELECT read_table( 'books' ) AS how_many;
>  resuts in
>  ERROR: parser: parse error at or near "$1"
>
> and the same using
> SELECT INTO res COUNT(id) FROM $1;
>  instead of
> SELECT INTO res COUNT(id) FROM table_name;
>  while
> SELECT INTO res COUNT(id) FROM books ( the real name of the table )
>  works good

You need to look into using EXECUTE if you want to specify tables
on the fly, and you may need something like:
create function read_table(text) returns int as '
declare
 table_name alias for $1;
 rec record;
begin
 for rec in EXECUTE ''select count(*) from '' || table_name LOOP
  return rec.count;
 END LOOP;
 return 0;
end;' language 'plpgsql';




^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2001-12-10 21:54 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2001-12-10 08:39 table name as parameter in pl/psql darthxiong@libero.it <darthxiong@libero.it>
2001-12-10 21:54 ` Stephan Szabo <sszabo@megazone23.bigpanda.com>

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