table name as function argument?

From: "Les Carter" <les(dot)carter(at)help-me(dot)to>
To: pgsql-novice(at)postgresql(dot)org
Subject: table name as function argument?
Date: 2005-02-06 02:21:04
Message-ID: 1107656463.10156@workhorse.newkinetics.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

This is a multi-part message in MIME format.

--bound1107656464
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 7bit

Hi, I'm fairly new to postgres and was wondering how would I go about using an argument passed into a user function to refer to a table.

As an example, consider the code below where I want to see if a table has any rows in it (this isn't what I'm trying to achieve but just the easiest thing I could think of to demonstrate what my problem is).

CREATE OR REPLACE FUNCTION istableempty("varchar", "char")
RETURNS bool AS
DECLARE
found int2;
BEGIN
SELECT into found count(*) FROM $1 WHERE rowid=$2;
IF found = 0
THEN
RETURN true;
ELSE
RETURN false;
END IF;
END

If I try to run, it tells me I have an error at or near the $1

Can anyone cast some light as to how I might be able to use an argument to refer to a table?

Thanks,

L

--bound1107656464--

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Michael Guerin 2005-02-07 01:31:22 Re: Function to blame?
Previous Message Tom Lane 2005-02-05 17:10:36 Re: Converting value to array