From: Stephan Szabo <sszabo@megazone23.bigpanda.com>
To: Matthew Nuzum <cobalt@bearfruit.org>
Cc: pgsql-sql@postgresql.org
Subject: Re: cannot create function that uses variable table name
Date: Thu, 16 Jan 2003 20:43:49 -0800 (PST)
Message-ID: <20030116204152.I15804-100000@megazone23.bigpanda.com> (raw)
In-Reply-To: <001d01c2bdd9$191b07f0$6700a8c0@mattspc>
On Thu, 16 Jan 2003, Matthew Nuzum wrote:
> I have a number of tables in my database that use the concept of
> display order, which is a field that can be used in an order by clause
> to dictate what order the results should come out in.
>
> I thought I would be crafty and devise a function that would always
> return the highest numbered item in the table. But it doesnt work. It
> always gives me a parse error at $1. Heres the function:
>
> CREATE OR REPLACE FUNCTION get_last_dsply_order(
> varchar, -- tablename
> varchar, -- id_col_name
> varchar) -- where_item
> RETURNS integer AS '
> DECLARE total_items integer;
> tablename ALIAS FOR $1;
> id_col_name ALIAS FOR $2;
> where_item ALIAS FOR $3;
> BEGIN
> SELECT INTO total_items count(*) FROM tablename WHERE id_col_name
> = where_item;
> RETURN total_items;
> END;
> ' LANGUAGE 'plpgsql';
You'll need to look into EXECUTE. You also are going to have to
watch out for concurrency issues since two transactions calling
this function at the same time for the same args are likely to
give incorrect results.
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: sszabo@megazone23.bigpanda.com, cobalt@bearfruit.org
Subject: Re: cannot create function that uses variable table name
In-Reply-To: <20030116204152.I15804-100000@megazone23.bigpanda.com>
* 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