- Categories:
Context functions (Session)
LAST_QUERY_ID¶
Returns the ID of a specified query in the current session. If no query is specified, the most recent query is returned.
Tip
Instead of using this function with the RESULT_SCAN function to process the results of a previous command, you can use the pipe operator. That way, you can run the command and process its result set in a single step.
Syntax¶
LAST_QUERY_ID( [ <num> ] )
Arguments¶
num
Specifies the query to return, based on the position of the query (within the session).
Default:
-1
Usage notes¶
Positive numbers start with the first query that was run in the session. For example:
LAST_QUERY_ID(1)
returns the first query.LAST_QUERY_ID(2)
returns the second query.LAST_QUERY_ID(6)
returns the sixth query.
Negative numbers start with the most recent query in the session. For example:
LAST_QUERY_ID(-1)
returns the most recent query (equivalent toLAST_QUERY_ID()
).LAST_QUERY_ID(-2)
returns the second most recent query.
Examples¶
Return the ID for the most recent query:
SELECT LAST_QUERY_ID();
Return the ID for the first query that was run in the session:
SELECT LAST_QUERY_ID(1);