RESULT_SCAN
functionsupportedQueries & DMLReturns the result set of a previous command (within 24 hours of when you ran the query) as if the result was a table. This function is particularly useful if you want to process the output from any of the following operations: - SHOW or DESC[RIBE] command that you ran. - Query that you ran on metadata or account usage information, such as Snowflake Information Schema or Account Usage. - The result of a stored procedure that you called. As an alternative to using RESULT_SCAN, you can call a stored procedure that returns tabular data in the FROM clause of a SELECT statement. The command or query can be from the current session or any of your other sessions, including past sessions, as long as the 24 hour period hasn’t elapsed. This period isn’t adjustable. For more information, see Using Persisted Query Results. Tip You can use the pipe operator (->>) instead of this function to process the results of a previous command.
Syntax
supportedpartialnot supportednot scored
RESULT_SCAN ( [ { '<query_id>' | <query_index> | LAST_QUERY_ID() } ] )
Parameters
| Name | Required | Description |
|---|---|---|
'query_id' or query_index or LAST_QUERY_ID() | required | A specification of a query that you ran within the last 24 hours in any session, an integer index of a query in the current session, or the LAST_QUERY_ID function, which returns the ID of a query within your current session. Snowflake query IDs are unique strings that resemble 01b71944-0001-b181-0000-0129032279f6. Query indexes are relative to the first query in the current session (if positive) or to the most recent query (if negative). For example, RESULT_SCAN(-1) is equivalent to RESULT_SCAN(LAST_QUERY_ID()). This argument is optional. If it is omitted, the default is RESULT_SCAN(-1), which returns the result set of the most recent command. |
Usage notes
- If the original query was run manually, access to its results is scoped to the user identity, not the active role. Only the user who ran the original query can use RESULT_SCAN to process its output. The same user can access these results after switching to another role assigned to them, regardless of which role was active when the original query ran. RESULT_SCAN doesn’t check the active role’s privileges on the source objects when retrieving these results. Even a user with the ACCOUNTADMIN role can’t access another user’s query results by calling RESULT_SCAN. When assigning multiple roles to a user, account for this user-scoped access to persisted query results. Switching roles doesn’t isolate the results of that user’s manually run queries.
- If the original query was run by using a task, the role that owns the task, instead of a specific user, triggered and ran the query. If a user or a task is operating with the same role, they can use RESULT_SCAN to access the query results.
- Snowflake stores all query results for 24 hours. This function only returns results for queries that were run within this time period.
- Result sets don’t have any metadata associated with them, so processing large results might be slower than if you were querying an actual table.
- The query containing the RESULT_SCAN can include clauses, such as filters and ORDER BY clauses, that weren’t in the original query. You can use these clauses to narrow down or modify the result set.
- A RESULT_SCAN isn’t guaranteed to return rows in the same order as the original query returned the rows. You can include an ORDER BY clause with the RESULT_SCAN query to specify a specific order.
- To retrieve the ID for a specific query, use any of the following methods: Snowsight: In either of the following locations, click the provided link to display or copy the ID: In Worksheets under Projects, after running a query, the Query Details include a link for the ID.
- In Query History under Monitoring, each query includes the ID as a link.
- SQL: Call one of the following functions: QUERY_HISTORY , QUERY_HISTORY_BY_* table function. LAST_QUERY_ID function (if the query was run in the current session). For example: Copy code SELECT LAST_QUERY_ID(-2); This is equivalent to using LAST_QUERY_ID as the input for RESULT_SCAN.
- If RESULT_SCAN processes query output that contained duplicate column names (for example, a query that joined two tables that have overlapping column names), then RESULT_SCAN references the duplicate columns with modified names, appending _1, _2, and so on to the original name. For an example, see the following Examples section.
- Timestamps in Parquet files that are queried by using the vectorized scanner sometimes display the time in a different time zone. Use the CONVERT_TIMEZONE function to convert to a standard time zone for all timestamp data.
Collation details
When RESULT_SCAN returns the results of the previous statement, RESULT_SCAN preserves the collation specification(s) of the values that it returns.