Insert multiple rows where not exists PostgresQL

后端 未结 2 1277
醉酒成梦
醉酒成梦 2021-01-05 00:19

I\'d like to generate a single sql query to mass-insert a series of rows that don\'t exist on a table. My current setup makes a new query for each record insertion similar t

2条回答
  •  悲哀的现实
    2021-01-05 01:11

    a_horse_with_no_name's answer actually has a syntax error, missing a final closing right parens, but other than that is the correct way to do this.

    Update: For anyone coming to this with a situation like mine, if you have columns that need to be type cast (for instance timestamps or uuids or jsonb in PG 9.5), you must declare that in the values you pass to the query:

    -- insert multiple if not exists
    -- where another_column_name is of type uuid, with strings cast as uuids
    -- where created_at and updated_at is of type timestamp, with strings cast as timestamps
    WITH data (id, some_column_name, another_column_name, created_at, updated_at) AS (
      VALUES
        (, , 'a5fa7660-8273-4ffd-b832-d94f081a4661'::uuid, '2016-06-13T12:15:27.552-07:00'::timestamp, '2016-06-13T12:15:27.879-07:00'::timestamp),
        (, , 'b9b17117-1e90-45c5-8f62-d03412d407dd'::uuid, '2016-06-13T12:08:17.683-07:00'::timestamp, '2016-06-13T12:08:17.801-07:00'::timestamp)
    )
    INSERT INTO table_name (id, some_column_name, another_column_name, created_at, updated_at)
    SELECT d.id, d.survey_id, d.arrival_uuid, d.gf_created_at, d.gf_updated_at
    FROM data d
    WHERE NOT EXISTS (SELECT 1 FROM table_name t WHERE t.id = d.id);
    

    a_horse_with_no_name's answer saved me today on a project, but had to make these tweaks to make it perfect.

提交回复
热议问题