14

As described in the PostgreSQL manual one can add multiple rows in a single INSERT statement:

INSERT INTO films (code, title, did, date_prod, kind) VALUES
    ('6717', 'Tampopo', 110, '1985-02-10', 'Comedy'),
    ('6120', 'The Dinner Game', 140, DEFAULT, 'Comedy');

Also a nested SELECT query like this works:

INSERT INTO films (code, title, did, date_prod, kind)
     SELECT table.code, 'Tampopo', 110, '1985-02-10', 'Comedy'
     FROM other_table AS table2;

However, I can't seem to figure out a way to do both at the same time:

INSERT INTO films (code, title, did, date_prod, kind) 
SELECT
    (table.code + 100, 'abc', NULL, t2.date_prod, t2.kind),
    (table.code + 200, 'xyz', NULL, t2.date_prod, t2.kind)
FROM other_table AS t2;

If other_table contained only (61717 | 'Tampopo' | NULL | '1985-02-10' | 'Comedy'), the result table would look like this:

 code | title |  did |   date_prod  |   kind
--------------------------------------------------
61817 | 'abc' | NULL | '1985-02-10' | 'Comedy'
61917 | 'xyz' | NULL | '1985-02-10' | 'Comedy'

This fiddle should explain it further...

Could you please point me to where I am going wrong. If possible, I would like to avoid multiple lookups in other_table AS t2 because it is a VIEW and it takes considerable time to construct.

2
  • table.code + 100 is also not going to work. code seems to be a character column, you can't use + with strings. Plus: what is the expected result of 'HG120' + 100 - that doesn't make sense. Commented Oct 18, 2015 at 17:58
  • @a_horse_with_no_name Uff. Sorry. Tried to make things clearer... It should have been an INT. Commented Oct 18, 2015 at 18:03

3 Answers 3

15

Generally you can use select with union:

insert into films (code, title, did, date_prod, kind) 
    select t2.code + 100, t2.title, NULL::float, t2.date_prod, t2.kind
    from other_table AS t2
union
    select t2.code + 200, t2.title, NULL::float, t2.date_prod, t2.kind
    from other_table AS t2;

In the particular case you described, you can use the unnest (array []):

insert into films (code, title, did, date_prod, kind) 
    select 
        unnest(array[t2.code + 100, t2.code + 200]), 
        t2.title, NULL::float, t2.date_prod, t2.kind
    from other_table AS t2
Sign up to request clarification or add additional context in comments.

5 Comments

The same problem applies to your solution :( Try uncommenting: sqlfiddle.com/#!15/4884e/3/0
Simple cast. See the edited answer. (You should give the table definition earlier in the question).
Ok last time I bother - promised :) Your second solution is interesting because with UNION it seems that the other_table VIEW is constructed twice, which is quite expensive in my case. Could the array be extended to the other values or the entire row?
There is no general rule, but the use of unnest(array[]) for several columns is a bit more complicated. However, you can always check what the result the select gives before using it in the insert. Or ask a new question ;)
@n1000 You could use the WITH statement in order to extract a common table from the query, So the query will start like WITH other_table AS (....)
6

You can use INSERT INTO ... UNION ALL:

INSERT INTO films (code, title, did, date_prod, kind)
SELECT t2.code + 100, t2.title, t2.did, t2.date_prod, t2.kind
FROM other_table AS t2
UNION ALL
SELECT t2.code + 200, t2.title, t2.did, t2.date_prod, t2.kind
FROM other_table AS t2;

9 Comments

@n1000 Updated, anyway the UNION ALL will do the job
will this cause the VIEW other_table to be constructed twice?
Probably not, but to be sure check execution plan
@n1000 Could you decide on one version? Now the title is hardcoded value abc and xyx or how it depends on table2?
Since you're just inserting NULL into did you can exclude it from the insert and select: sqlfiddle.com/#!15/eeee1/1/0
|
-1

use construction

insert into table(column1)
((select uuid from table2))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.