pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Michael Fuhr <mike@fuhr.org>
To: hu js <hu.bill2005@hotmail.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: How to declare cursor if tablename is a variable?
Date: Mon, 19 Mar 2007 21:41:11 -0600
Message-ID: <20070320034111.GA23662@winnie.fuhr.org> (raw)
In-Reply-To: <BAY102-F146D8632913581B7A9AE40F0750@phx.gbl>
References: <20070314140934.GA7086@winnie.fuhr.org>
	<BAY102-F146D8632913581B7A9AE40F0750@phx.gbl>

[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



view thread (4+ messages)  latest in thread

Message-ID: <20070320034111.GA23662@winnie.fuhr.org>
Permalink:  ../20070320034111.GA23662@winnie.fuhr.org/
Also on:    postgresql.org/message-id/20070320034111.GA23662@winnie.fuhr.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: mike@fuhr.org, hu.bill2005@hotmail.com
  Subject: Re: How to declare cursor if tablename is a variable?
  In-Reply-To: <20070320034111.GA23662@winnie.fuhr.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