Sign In | Register Now About Bytes | Help | Site Map
Connecting Tech Pros Worldwide

Table name as parameter in function

Question posted by: Alexander Pucher (Guest) on November 23rd, 2005 02:03 AM
Hi,

struggling around with this for some time:

How can I use a table name as a parameter in a PL/pgSQL function ??

I tried this but it didn't work...


CREATE OR REPLACE FUNCTION my_row_count(text) RETURNS int4 AS '
DECLARE
num_rows int4;
BEGIN
num_rows := (select count(*) from $1);
RETURN num_rows;
END;
' LANGUAGE plpgsql;

Thnaks for any input!

regards,
alex.

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to Join Bytes!

Timothy Perrigo's Avatar
Timothy Perrigo
Guest
n/a Posts
November 23rd, 2005
02:03 AM
#2

Re: Table name as parameter in function
You'll need to use the EXECUTE command to build the SQL dynamically.

See:
http://www.postgresql.org/docs/7.4/interactive/plpgsql-
statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN

On Nov 23, 2004, at 2:56 PM, Alexander Pucher wrote:
[color=blue]
> Hi,
>
> struggling around with this for some time:
>
> How can I use a table name as a parameter in a PL/pgSQL function ??
>
> I tried this but it didn't work...
>
>
> CREATE OR REPLACE FUNCTION my_row_count(text) RETURNS int4 AS '
> DECLARE
> num_rows int4;
> BEGIN
> num_rows := (select count(*) from $1);
> RETURN num_rows;
> END;
> ' LANGUAGE plpgsql;
>
> Thnaks for any input!
>
> regards,
> alex.
>
> ---------------------------(end of
> broadcast)---------------------------
> TIP 1: subscribe and unsubscribe commands go to
> Join Bytes!
>[/color]


---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org


 
Not the answer you were looking for? Post your question . . .
189,879 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
  • Didn't find the answer you were looking for?
    Post Your Question
  • Top Community Contributors