This SQLite post explains how to use the SQLite last_insert_rowid function with syntax and examples.
The SQLite last_insert_rowid function returns the ROWID of the last INSERT in the database for the current session.
The syntax for the last_insert_rowid function in SQLite is:
There are no parameters or arguments for the last_insert_rowid function.
The last_insert_rowid function can be used in the following versions of SQLite:
Let's look at some SQLite last_insert_rowid function examples and explore how to use the last_insert_rowid function in SQLite.
For example, if we had the following employees table:
And the employees table contained the following records:
employee_id | last_name | first_name |
---|---|---|
1000 | Smith | Jane |
1001 | Mark | Dave |
And we executed the following INSERT statement:
The employees table would now look like this:
employee_id | last_name | first_name |
---|---|---|
1000 | Smith | Jane |
1001 | Mark | Dave |
1002 | Jones | Sarah |
And if we executed the last_insert_rowid function as follows:
The last_insert_rowid function would return 3 since the last INSERT statement inserted a record into the employees table with a ROWID of 3.