pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
PL/PGSQL - How to pass in variables?
4+ messages / 3 participants
[nested] [flat]

* PL/PGSQL - How to pass in variables?
@ 2006-05-14 15:28  Scott Yohonn <syohonn@gmail.com>
  0 siblings, 2 replies; 4+ messages in thread

From: Scott Yohonn @ 2006-05-14 15:28 UTC (permalink / raw)
  To: pgsql-sql

Using PL/PGSQL, I am trying to create a procedure to display the
count of rows in any single table of a database. The End-user would
pass in a table name and the prodecure would display the table name
with the row count.
I am able to hardcode the variable for table and get the appropriate
results from my count function (see below), but cannot pass in a
variable and have the function work. Any suggesstions???

CREATE FUNCTION get_table_count(tablename text) RETURNS integer AS $$
DECLARE

     --tablename ALIAS FOR $1;

      rowcount INTEGER;
   BEGIN

     SELECT INTO rowcount count(*) FROM tablename;

     RETURN rowcount;

   END;
 $$ LANGUAGE 'plpgsql';

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

* Re: PL/PGSQL - How to pass in variables?
@ 2006-05-14 15:44  Jean-Paul Argudo <jean-paul@argudo.org>
  parent: Scott Yohonn <syohonn@gmail.com>
  1 sibling, 1 reply; 4+ messages in thread

From: Jean-Paul Argudo @ 2006-05-14 15:44 UTC (permalink / raw)
  To: Scott Yohonn <syohonn@gmail.com>; +Cc: 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



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

* Re: PL/PGSQL - How to pass in variables?
@ 2006-05-14 16:16  Jaime Casanova <systemguards@gmail.com>
  parent: Scott Yohonn <syohonn@gmail.com>
  1 sibling, 0 replies; 4+ messages in thread

From: Jaime Casanova @ 2006-05-14 16:16 UTC (permalink / raw)
  To: Scott Yohonn <syohonn@gmail.com>; +Cc: pgsql-sql

On 5/14/06, Scott Yohonn <syohonn@gmail.com> wrote:
>
> Using PL/PGSQL, I am trying to create a procedure to display the
> count of rows in any single table of a database. The End-user would
> pass in a table name and the prodecure would display the table name
> with the row count.
> I am able to hardcode the variable for table and get the appropriate
> results from my count function (see below), but cannot pass in a
> variable and have the function work. Any suggesstions???
>
> CREATE FUNCTION get_table_count(tablename text) RETURNS integer AS $$
> DECLARE
>
>      --tablename ALIAS FOR $1;
>
>       rowcount INTEGER;
>    BEGIN
>
>      SELECT INTO rowcount count(*) FROM tablename;
>
>      RETURN rowcount;
>
>    END;
>  $$ LANGUAGE 'plpgsql';
>

you can't do this because tablename is a variable not a table, you
have to append the content of the variable in a string that can be
EXECUTE'd

     EXECUTE 'SELECT count(*) FROM ' || tablename INTO rowcount;

-- 
regards,
Jaime Casanova

"Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs and the universe trying
to produce bigger and better idiots.
So far, the universe is winning."
                                       Richard Cook



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

* Re: PL/PGSQL - How to pass in variables?
@ 2006-05-14 18:50  Jean-Paul Argudo <jean-paul@argudo.org>
  parent: Jean-Paul Argudo <jean-paul@argudo.org>
  0 siblings, 0 replies; 4+ messages in thread

From: Jean-Paul Argudo @ 2006-05-14 18:50 UTC (permalink / raw)
  To: Scott Yohonn <syohonn@gmail.com>; +Cc: pgsql-sql

Scott Yohonn wrote:
> Jean-Paul,
> 
> Thanks! This did work. The output put the name of the function
> (get_table_count) as the header. How would I display the name of the table
> that I am requesting the row count of?

The only way I know is to alias the output in the query calling the
function, so:


select get_table_count('bar') as bar;

 bar
-----
   3
(1 row)

I don't know any other way to do that...

Cheers,

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



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


end of thread, other threads:[~2006-05-14 18:50 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2006-05-14 15:28 PL/PGSQL - How to pass in variables? Scott Yohonn <syohonn@gmail.com>
2006-05-14 15:44 ` Jean-Paul Argudo <jean-paul@argudo.org>
2006-05-14 18:50   ` Jean-Paul Argudo <jean-paul@argudo.org>
2006-05-14 16:16 ` Jaime Casanova <systemguards@gmail.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