Re: How to declare cursor if tablename is a variable?

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: hu js <hu(dot)bill2005(at)hotmail(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: How to declare cursor if tablename is a variable?
Date: 2007-03-20 03:41:11
Message-ID: 20070320034111.GA23662@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

[Please copy the mailing list on replies so others can participate
in and learn from the discussion.]

On Tue, Mar 20, 2007 at 02:28:15AM +0000, hu js wrote:
> It doesn't work. Because can't use variable for table name in query.
>
> Please tell me another way. Thanks

What exactly have you tried that doesn't work? Will neither of the
following do what you want? What version of PostgreSQL are you using?

DECLARE
rec_obj record;
query_obj text := 'SELECT mrc_xy_position FROM ' || quote_ident(tablename);
BEGIN
FOR rec_obj IN EXECUTE query_obj LOOP
[...]
END LOOP;

or

DECLARE
cur_obj refcursor;
query_obj text := 'SELECT mrc_xy_position FROM ' || quote_ident(tablename);
BEGIN
OPEN cur_obj FOR EXECUTE query_obj;
[...]

--
Michael Fuhr

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message remco lengers 2007-03-20 09:11:41 ERROR: duplicate key violates unique constraint
Previous Message Paul Lambert 2007-03-20 03:21:45 Re: Issue with copying data from a text file.