Skip to content
Advertisement

Sqlite copy data from temp table to another with conflict

i have db sqlite with main table and temp table. I want to import data from text file and then i want to insert data from temp table to main table.

INSERT INTO table(key) SELECT src.key FROM temp as src
ON CONFLICT (table.key) DO UPDATE SET to_insert = 0, to_delete =0;

When i am trying run this query then sqlite throw me this error.

Query 1 ERROR: near “DO”: syntax error

Is possible insert data from another table with conflicts?

Advertisement

Answer

The select statement needs a where clause:

insert into mytable(key) 
select key from temp where true
on conflict (key) do update set to_insert = 0, to_delete =0;

This pitfall is documented as parsing ambiguity (emphasis mine):

When the INSERT statement to which the UPSERT is attached takes its values from a SELECT statement, there is a potential parsing ambiguity. The parser might not be able to tell if the ON keyword is introducing the UPSERT or if it is the ON clause of a join. To work around this, the SELECT statement should always include a WHERE clause, even if that WHERE clause is just WHERE true.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement