Re: PL/PGSQL - How to pass in variables?

From: Jean-Paul Argudo <jean-paul(at)argudo(dot)org>
To: Scott Yohonn <syohonn(at)gmail(dot)com>
Cc: "pgsql-sql(at)postgresql(dot)org" <pgsql-sql(at)postgresql(dot)org>
Subject: Re: PL/PGSQL - How to pass in variables?
Date: 2006-05-14 15:44:50
Message-ID: 44675072.8030104@argudo.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hi Scott,

You'll have to execute dynamic SQL (see doc chapter "36.6.5. Executing
Dynamic Commands") for your function to work:

CREATE FUNCTION get_table_count(tablename text) RETURNS integer AS
$$
DECLARE
--tablename ALIAS FOR $1;
rowcount INTEGER;
BEGIN

execute 'SELECT count(*) FROM '||tablename into rowcount;
return rowcount;

END;
$$ LANGUAGE 'plpgsql';

select get_table_count('bar');

get_table_count
-----------------
3
(1 row)

Cheers,

--
Jean-Paul Argudo
www.PostgreSQLFr.org
www.dalibo.com

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Jaime Casanova 2006-05-14 16:16:58 Re: PL/PGSQL - How to pass in variables?
Previous Message Scott Yohonn 2006-05-14 15:28:58 PL/PGSQL - How to pass in variables?