Re: How to declare cursor if tablename is a variable? - Mailing list pgsql-sql

From Michael Fuhr
Subject Re: How to declare cursor if tablename is a variable?
Date
Msg-id 20070320034111.GA23662@winnie.fuhr.org
Whole thread Raw
In response to Re: How to declare cursor if tablename is a variable?  (Michael Fuhr <mike@fuhr.org>)
List 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


pgsql-sql by date:

Previous
From: Paul Lambert
Date:
Subject: Re: Issue with copying data from a text file.
Next
From: "remco lengers"
Date:
Subject: ERROR: duplicate key violates unique constraint