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 recently-executed query is returned.

See also:

RESULT_SCAN

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 executed 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.

    • Etc.

  • Negative numbers start with the most recently-executed query in the session. For example:

    • LAST_QUERY_ID(-1) returns the most recently-executed query (equivalent to LAST_QUERY_ID()).

    • LAST_QUERY_ID(-2) returns the second most recently-executed query.

    • Etc.

Examples

Return the ID for the most recently-executed query:

SELECT LAST_QUERY_ID();

Return the ID for the first query executed in the session:

SELECT LAST_QUERY_ID(1);