pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Jean-Paul Argudo <jean-paul@argudo.org>
To: Scott Yohonn <syohonn@gmail.com>
Cc: pgsql-sql@postgresql.org <pgsql-sql@postgresql.org>
Subject: Re: PL/PGSQL - How to pass in variables?
Date: Sun, 14 May 2006 17:44:50 +0200
Message-ID: <44675072.8030104@argudo.org> (raw)
In-Reply-To: <ff1df1410605140828s2ac7c170j9d7c4e5b2b0a1f5c@mail.gmail.com>
References: <ff1df1410605140828s2ac7c170j9d7c4e5b2b0a1f5c@mail.gmail.com>

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



view thread (4+ messages)  latest in thread

Message-ID: <44675072.8030104@argudo.org>
Permalink:  ../44675072.8030104@argudo.org/
Also on:    postgresql.org/message-id/44675072.8030104@argudo.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: jean-paul@argudo.org, syohonn@gmail.com
  Subject: Re: PL/PGSQL - How to pass in variables?
  In-Reply-To: <44675072.8030104@argudo.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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