Column names for INSERT with query

Started by Robert Jamesover 12 years ago5 messagesgeneral
Jump to latest
#1Robert James
srobertjames@gmail.com

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?

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

#2bricklen
bricklen@gmail.com
In reply to: Robert James (#1)
Re: Column names for INSERT with query

On Thu, Aug 22, 2013 at 3:30 PM, Robert James <srobertjames@gmail.com>wrote:

I would like to:

INSERT INTO t
SELECT * FROM r, (x + y) AS field3

INSERT INTO t (col1, col2, col3)
SELECT ...

#3Ian Lawrence Barwick
barwick@gmail.com
In reply to: Robert James (#1)
Re: Column names for INSERT with query

2013/8/23 Robert James <srobertjames@gmail.com>:

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?

INSERT INTO t (t_field1, t_field2, t_field3)
SELECT r.field1, r.field2, (x+y)
FROM r

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

#4BladeOfLight16
bladeoflight16@gmail.com
In reply to: Ian Lawrence Barwick (#3)
Re: Column names for INSERT with query

On Thu, Aug 22, 2013 at 6:36 PM, Ian Lawrence Barwick <barwick@gmail.com>wrote:

INSERT INTO t (t_field1, t_field2, t_field3)
SELECT r.field1, r.field2, (x+y)
FROM r

Obligatory link to documentation:
http://www.postgresql.org/docs/9.2/static/sql-insert.html. See where it
says [ ( *column_name* [, ...] ) ]? The square brackets mean "optional",
and the ",..." means repeat as needed. The fact "column_name" is italics
tell you it's some kind of variable text you should replace, and later on
in the page explains what goes there. So it's telling you that you can use
a comma separated list of column names from the destination table.
PostgreSQL has some of the best documentation of any software I've ever
seen. Don't hesitate to check it often when working.

#5David G. Johnston
david.g.johnston@gmail.com
In reply to: Robert James (#1)
Re: Column names for INSERT with query

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.

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general