Re: Column names for INSERT with query

From: David Johnston <polobo(at)yahoo(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Column names for INSERT with query
Date: 2013-08-23 14:32:47
Message-ID: 1377268367186-5768388.post@n5.nabble.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Robert James wrote
> I would like to:
>
> INSERT INTO t
> SELECT * FROM r, (x + y) AS field3
>
> How do I correlate the names of the fields? That is, how do I indicate
> which fields from r or field3 should be inserted into the right
> columns in t?

Already answered but I'll add that as written, assuming there are enough
columns present, the syntax shown will work and do a pair-wise matching of
columns in the order defined in the catalog for table "t". You can view
that order by simply issuing a "SELECT * FROM t". If you know the order of
"t" you can simply explode the "SELECT * FROM r, (x+y)" into an explicit
column-list that has the same ordering as "t". There are limitations,
mainly as relates to default values, but for a table "t" with a large number
of columns it can be considerably less verbose/tedious to write.

David J.

--
View this message in context: http://postgresql.1045698.n5.nabble.com/Column-names-for-INSERT-with-query-tp5768334p5768388.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Hall, Samuel L (Sam) 2013-08-23 15:04:11 Update quey
Previous Message Osvaldo Kussama 2013-08-23 14:07:19 Re: numeric to ipv6 inet