result_scan

Applies to: ✅ Data 360 SQL

Returns the result set of a completed query as a table. Use result_scan in the FROM clause to query cached results without rerunning the original query.

Syntax 

1result_scan('<query_id>')

Arguments 

Required 

  • <query_id>: The query identifier returned by the Data 360 Query API when the source query was submitted. Pass it as a string literal. You can’t use a column reference or a computed expression.

Returns 

Returns a set of rows whose column names and data types match the result set of the referenced query. Use it anywhere a table reference is valid in a FROM clause.

Considerations 

result_scan is a set-returning function that must appear in the FROM clause, not in a SELECT list or WHERE clause.

Execution and Behavior 

  • The referenced query must complete successfully and produce a result set before you can call result_scan against it. Queries that are still running, have failed, don’t exist, or return no rows result in an error.
  • When you submit a query using the Data 360 Query API, store the returned queryId and pass it to subsequent result_scan calls. A single query can contain multiple calls referencing the same or different IDs.
  • Rows returned by result_scan might not match the order of the original query. Use an explicit ORDER BY clause if sorting matters.

Constraints and Retention 

  • Data 360 retains query results for up to 24 hours, though they may become unavailable sooner due to cache eviction. If results expire or are evicted, resubmit the original query to regenerate the result set.
  • You can only scan results from queries that you submitted within your current data space. Referencing a query issued by another user or running in a different data space returns an error.

Errors 

SQLSTATEConditionMessage
42704Query ID not found, not yours, not finished, or expiredThe result of the query '<queryId>' cannot be scanned. Either the query id does not exist, the query was issued by another user, the query execution has not finished yet or the result expired.
42704Query still runningThe result of the query '<queryId>' cannot be scanned. The query execution has not finished yet.
22023Source query produced no result setThe query '<queryId>' did not produce a result.

Examples 

Because result_scan returns the schema of the source query, expected output depends on your data. The examples below show query structure only.

Query a Cached Result Set 

Submit an initial query and store the returned queryId. Use result_scan to aggregate the cached results without rerunning the source query. Pass the queryId value (for example, '3442db1a-ecef-42b3-b0d8-971ee459efa4') returned by the Data 360 Query API.

1SELECT region, SUM(revenue) AS total_revenue
2FROM result_scan('3442db1a-ecef-42b3-b0d8-971ee459efa4')
3GROUP BY region
4ORDER BY total_revenue DESC;

Filter Results from a Large Cached Query 

Filter results from a completed query without rescanning the source table.

1SELECT customer_id, order_date, amount
2FROM result_scan('3442db1a-ecef-42b3-b0d8-971ee459efa4')
3WHERE amount > 500;

Join Two Cached Result Sets 

Reference two different cached result sets in a single query.

1SELECT a.customer_id, a.segment, b.total_spend
2FROM result_scan('3442db1a-ecef-42b3-b0d8-971ee459efa4') AS a
3JOIN result_scan('7f9c21ba-ab12-4d3c-9e45-0b8c6d2f1a3e') AS b
4  ON a.customer_id = b.customer_id
5WHERE b.total_spend > 1000;

Related Documentation