Canceling Statements¶
The recommended way to cancel a statement is to use the interface of the application in which the query is running (e.g. the Worksheet in the Snowflake web interface) or the cancellation API provided by the Snowflake ODBC or JDBC driver. However, in some cases, it is necessary to cancel a query using SQL.
Snowflake provides the following functions to support using SQL to cancel running/active statements:
Example¶
The following Java sample code uses SYSTEM$CANCEL_ALL_QUERIES and other Snowflake functions to cancel a running statement in the current session after 5 seconds:
The sample code first issues a SQL command for CURRENT_SESSION to obtain the session identifier.
It then creates a task to be executed 5 seconds later. This task uses the session identifier as a parameter to SYSTEM$CANCEL_ALL_QUERIES.
Then a long running statement is executed using the GENERATOR table function to generate rows for 120 seconds.