Question

In a large database, I'm using a very long query similar to this:

INSERT INTO t VALUES (@p_1); SELECT last_insert_rowid() as i_1;
INSERT INTO t VALUES (@p_2); SELECT last_insert_rowid() as i_2;
...

This allows for infinitely faster insertion compared to single transactions. However, I was surprised that only one value is returned instead of one per SELECT.

Usually, this is a non-issue because it is easy to use only one rowid and calculate the others from it (autoincrement issues may be an exception). However, for the sake of understanding (and disregarding that I probably abuse SQLite): Why is this happening, and how could I still use multiple SELECTs in a single transaction?

Was it helpful?

Solution

try with temp table like below

PRAGMA temp_store = MEMORY;
CREATE TEMP TABLE TempIds (id INTEGER);

INSERT INTO t VALUES (@p_1); INSERT INTO TempIds VALUES(last_insert_rowid());
INSERT INTO t VALUES (@p_2);  INSERT INTO TempIds VALUES(last_insert_rowid());
...

SELECT id FROM TempIds;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top