Skip to content Skip to sidebar Skip to footer

Postgresql Insert From Select With Additional Column

I have table T1 in database DB1 and table T2 in database DB2, those tables have almost identical sets of columns except column C_additional in T1, which is not present in T2. I nee

Solution 1:

You can specify the target columns with parenthesis before the select clause:

INSERTINTO T1
(c1, c_additional) -- hereSELECT 
        C1,
        'needed_value'-- just select a constant hereFROM dblink(
    'hostaddr=127.0.0.1 port=5432 dbname=DB2 user=postgres password=postgres', 
    'SELECT * FROM T2')
AS T2_row(C1 integer) 

Solution 2:

Can you try this?

INSERTINTO T1
SELECT 
        C1,
        'needed_value'FROM dblink(
    'hostaddr=127.0.0.1 port=5432 dbname=DB2 user=postgres password=postgres', 
    'SELECT * FROM T2')
AS T2_row(C1 integer)

Post a Comment for "Postgresql Insert From Select With Additional Column"