table name as parameter in pl/psql

From: darthxiong(at)libero(dot)it <darthxiong(at)libero(dot)it>
To: pgsql-admin(at)postgresql(dot)org
Subject: table name as parameter in pl/psql
Date: 2001-12-10 08:39:29
Message-ID: GO4DDT$IwbN_cz2aIDQkZtyjXg48hRwnNbgm9xc6SbNmYLa0qiE2gZ@libero.it
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

Responses

Browse pgsql-admin by date

  From Date Subject
Next Message Ewan Leith 2001-12-10 14:52:05 Moving a database
Previous Message Phill Kenoyer 2001-12-08 04:06:43 Re: Primary Key Problems