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 theUPSERT
is attached takes its values from aSELECT
statement, there is a potential parsing ambiguity. The parser might not be able to tell if theON
keyword is introducing theUPSERT
or if it is theON
clause of a join. To work around this, theSELECT
statement should always include aWHERE
clause, even if thatWHERE
clause is justWHERE true
.