Connecting Tech Pros Worldwide Help | Site Map

Table name as parameter in function

  #1  
Old November 23rd, 2005, 03:03 AM
Alexander Pucher
Guest
 
Posts: n/a
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 majordomo@postgresql.org

  #2  
Old November 23rd, 2005, 03:03 AM
Timothy Perrigo
Guest
 
Posts: n/a

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
> majordomo@postgresql.org
>[/color]


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

http://archives.postgresql.org

Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Table name as parameter in SQL query, with VBA... laurentc answers 5 September 12th, 2006 05:55 PM
MySQLdb question... using table name as arg Sean Berry answers 4 February 3rd, 2006 11:15 PM
STORED PROCEDURE - passing table name as a parameter Steve answers 15 December 2nd, 2005 04:05 PM
get current table name in stored procedure Michael Spiegel answers 6 July 20th, 2005 05:05 AM