CURSOR

Description

For more information regarding the Cursor declaration, check here.

Cursors are pointers that allow users to iterate through query results. For more information on Oracle Cursors check here.

Cursor Definition

CURSOR cursor
 [ ( cursor_parameter_dec [, cursor_parameter_dec ]... )]
   [ RETURN rowtype] IS select_statement ;

Cursor Open

OPEN cursor [ ( cursor_parameter [ [,] actual_cursor_parameter ]... ) ] ;

Cursor Fetch

FETCH { cursor | cursor_variable | :host_cursor_variable }
  { into_clause | bulk_collect_into_clause [ LIMIT numeric_expression ] } ;

Cursor Close

CLOSE { cursor | cursor_variable | :host_cursor_variable } ;

Cursor Attributes

named_cursor%{ ISOPEN | FOUND | NOTFOUND | ROWCOUNT }

Cursor FOR Loop

[ FOR record IN
  { cursor [ ( cursor_parameter_dec
               [ [,] cursor_parameter_dec ]... )]
  | ( select_statement )
  }
    LOOP statement... END LOOP [label] ;

Snowflake Scripting has support for cursors, however, they have fewer functionalities compared to Oracle. To check more information regarding these cursors, check here.

Cursor Declaration

Cursor Open

Cursor Fetch

Cursor Close

Cursor FOR Loop

Sample Source Patterns

1. Basic cursor example

2. Explicit Cursor For Loop

3. Implicit Cursor For Loop

4. Parameterized Cursor

You can use "?" In the filter condition of the cursor at the declaration section define the bind variable. While opening the cursor we can add the additional syntax “USING <bind_variable_1 >” to pass the bind variable.

Below are some examples of scenarios that can occur in the use of parameters in cursors:

4.1 Basic Cursor Parameterized Example

4.2 Parameterized Cursors With Multiple Sending Parameters

4.3 Parameterized Cursors With Use Of Procedure Parameters In Query

5. Using Cursors In Fetch And For Loop

Cursors can be controlled through the use of the FOR statement, allowing each and every record of a cursor to be processed while the FETCH statement puts, record by record, the values returned by the cursor into a set of variables, which may be PLSQL records

5.1 Cursors For Loop

5.2 Cursors Fetch

Known Issues

1. RETURN clause is not supported in Snowflake Scripting Cursor Declaration

The Cursor Declaration for Snowflake Scripting does not include this clause. It can be removed from the Oracle Cursor definition to get functional equivalence.

2. OPEN statement cannot pass values for declared arguments

Even though arguments can be declared for a cursor, their values cannot be assigned in Snowflake Scripting. The best alternative is to use the USING clause with bind variables.

3. FETCH statement cannot use records

Snowflake Scripting does not support records. However, it is possible to migrate them using the OBJECT data type and the OBJECT_CONSTRUCT() method. For more information please see the Record Type Definition Section.

4. FETCH BULK COLLECT INTO clause is not supported in Snowflake Scripting

Snowflake Scripting does not support the BULK COLLECT INTO clause. However, it is possible to use ARRAY_AGG along with a temporal table to construct a new variable with the data corresponding to the Cursor information. For more information please see the Collection Bulk Operations Section.

5. Cursor attributes do not exist in Snowflake Scripting

Oracle cursors have different attributes that allow the user to check their status like if it is opened or the amount of fetched rows, however, these attributes regarding the cursor status do not exist in Snowflake Scripting.

6. The cursor's query does not have access to the procedure's variables and parameters

In Oracle, the query in the cursor declaration has access to procedure variables and parameters but in Snowflake Scripting, it does not. The alternative to this is to use the USING clause with bind variables. For more information check this section.

7. %NOTFOUND attribute is not supported in Snowflake Scripting Cursor

In Oracle can be used, before the first fetch from an open cursor, cursor_name%NOTFOUND returns TRUE if the last fetch failed to return a row, or FALSE if the last fetch returned a row. Snowflake Scripting does not support the use of this attribute instead it can be validated if the variable assigned to the cursor result contains values

  1. MSCEWI3106: Parametrized Cursor is not supported by Snowflake Scripting.

  2. MSCEWI3130: For Loop and Fetch into a variable cannot be used on the same cursor.

  3. MSCCP0003: This statement has usages of cursor for loop.

  4. MSCCP0006: Fetch inside a loop is considered a complex pattern, this could degrade Snowflake performance.

Last updated

Was this helpful?