Skip site navigation (1) Skip section navigation (2)

Re: Table name as a variable and/or EXECUTE and NEW.*

From: Ken LaCrosse <klacross(at)raleys(dot)com>
To: Josh Kupershmidt <schmiddy(at)gmail(dot)com>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: Table name as a variable and/or EXECUTE and NEW.*
Date: 2012-06-18 17:34:07
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-novice
That worked great Josh.  Woohoo!


Ken LaCrosse | Enterprise Architect
500 West Capitol Avenue
West Sacramento, California 95605-2696
   P: 916.373.6571 | F: 916.373.6553
   E: klacross <klacross(at)raleys(dot)com> <rjacobsen(at)raleys(dot)com>

We're just here to help.™ Visit for blogs, recipes and

On Fri, Jun 15, 2012 at 4:18 PM, Josh Kupershmidt <schmiddy(at)gmail(dot)com>wrote:

> On Thu, Jun 14, 2012 at 5:29 PM, Ken LaCrosse <klacross(at)raleys(dot)com> wrote:
> > I'm trying to build a trigger which will dynamically create SQL
> statements in pgplsql that reference different table names (for child
> tables of a partitions parent table).
> > Can I do this without performing an EXECUTE?
> If the table your trigger is inserting into must be computed
> dynamically, you probably need to use EXECUTE.
> > If not how do I need to reference NEW.* in the SQL command?  Basically
> if I don't use an EXECUTE then postgres doesn't like the variable used
> where a table named is expected and if I do use an EXECUTE then it doesn't
> like the reference to NEW.*.
> >
> > Here's the code:
> > CREATE OR REPLACE FUNCTION fp_eitem_price_insert_trigger()
> >     sql varchar;
> >     child varchar := get_TblName(NEW.store_number, NEW.effective_date);
> > --    sql := 'INSERT INTO ' || child || ' VALUES (NEW.*);';
> > --    EXECUTE(sql);
> > --    INSERT INTO fp_ep_113_20120530 VALUES (NEW.*);
> [snip]
> Try the EXECUTE ... USING construct, available in Postgres 8.4+, like so:
>  EXECUTE 'INSERT INTO ' || quote_ident(child) || ' SELECT $1.*' USING NEW;
> You might want to specify the schema name of the child table
> explicitly as well. See also this similar question on stackoverflow:
> Josh

Warning:  this e-mail may contain information proprietary to Raley's
and is intended only for the use of the intended recipient(s).  
If the read of this message is not an intended recipient,
you are hereby notified that you have received this message in
error and that any review, dissemination, distribution, or copying
of this message is strictly prohibited.  If you have received this message
in error, please notify the sender immediately.

In response to

pgsql-novice by date

Next:From: Oliver d'Azevedo ChristinaDate: 2012-06-18 17:54:53
Subject: Re: select from multiple tables
Previous:From: Alessandro GagliardiDate: 2012-06-18 16:43:01
Subject: Re: select from multiple tables

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group