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
INSERTstatement to which theUPSERTis attached takes its values from aSELECTstatement, there is a potential parsing ambiguity. The parser might not be able to tell if theONkeyword is introducing theUPSERTor if it is theONclause of a join. To work around this, theSELECTstatement should always include aWHEREclause, even if thatWHEREclause is justWHERE true.