How to run cursor in snowflake?

Solution 1:

Snowflake Scripting introduced support for cursors.

Working with Cursors

You can use a cursor to iterate through query results one row at a time.

Introduction

To use a cursor, do the following:

  1. In the DECLARE section, declare the cursor. The declaration includes the query for the cursor.

  2. Execute the OPEN command to open the cursor. This executes the query and loads the results into the cursor.

  3. Execute the FETCH command to fetch one or more rows and process those rows.

  4. When you are done with the results, execute the CLOSE command to close the cursor.

Solution 2:

Snowflake SQL does not support native SQL cursors. I believe you'll want to leverage a Javascript UDF, UDTF, or Stored Procedure (depending on what you want your output to be). Based on your example above, I believe a Javascript UDTF is what you need: https://docs.snowflake.net/manuals/sql-reference/udf-js-table-functions.html