Deepak Kumar
Deepak Kumar

Reputation: 3751

Bulk insert in more than one column in PostgreSQL

I have one bulk insert query

insert into table1(shift) select value from table2 where transid='shiftid'

This query is inserting all the shift values from table2 into my table1 shift column.

But what if I want to insert record in multiple columns in table1 but my select query will return only one column, say:

select value from table1 where transid in ('shiftid','gradeid','currencyid')

It will return one column with all the values. But I want to insert:

insert into table1(shift,grade,currency) ...........

Can anyone fill the .... for me? I hope you understand my requirement.

Upvotes: 0

Views: 400

Answers (1)

mu is too short
mu is too short

Reputation: 434585

I don't know what your tables look like but all you need is to make sure that the number of columns in the INSERT and SELECT match and that they have compatible types. So something like this:

insert into table1 (shift, grade, currency)
select shift_id, grade_id, currency_id
from table2
where transid in ('shiftid', 'gradeid', 'currencyid')

The columns in the SELECT depend on what table2 looks like.

Upvotes: 1

Related Questions