Bulk Cursor Helpers
These procedures and functions are used to simulate the behavior of a CURSOR with bulk operations.
The Cursor is simulated with an OBJECT with different information regarding the state of the cursor. A temporary table is created to store the result set of the cursor's query.
Most of these Procedures return a new Object with the updated state of the cursor.
INIT_CURSOR
This function initializes a new object with the basic cursor information
CREATE OR REPLACE FUNCTION INIT_CURSOR(NAME VARCHAR, QUERY VARCHAR)
RETURNS OBJECT
AS
$$
SELECT OBJECT_CONSTRUCT('NAME', NAME, 'ROWCOUNT', -1, 'QUERY', QUERY, 'ISOPEN', FALSE, 'FOUND', NULL, 'NOTFOUND', NULL)
$$;OPEN_BULK_CURSOR_UDF
These procedures creates a temporary table with the query of the cursor. An optional overload exists to support bindings.
CREATE OR REPLACE PROCEDURE OPEN_BULK_CURSOR_UDF(CURSOR OBJECT, BINDINGS ARRAY)
RETURNS OBJECT
LANGUAGE JAVASCRIPT
EXECUTE AS CALLER
AS
$$
var query = `CREATE OR REPLACE TEMPORARY TABLE ${CURSOR.NAME}_TEMP_TABLE AS ${CURSOR.QUERY}`;
snowflake.execute({ sqlText: query, binds: BINDINGS });
CURSOR.ROWCOUNT = 0;
CURSOR.ISOPEN = true;
return CURSOR;
$$;CLOSE_BULK_CURSOR_UDF
This procedure deletes the temporary table that stores the result set of the cursor and resets the cursor's properties to their initial state.
FETCH Helpers
Due to Oracle being capable of doing the FETCH statement on different kind of scenarios, a multiple procedures with overloads were created to handle each case. These helpers save the fetched values into the RESULT property in the CURSOR object.
Some of the overloads include variations when the LIMIT clause was used or not. Other overloads have a COLUMN_NAMES argument that is necessary when the FETCH statement is being done into a variable that has or contains a records with column names that are different to the column names of the query.
FETCH_BULK_COLLECTION_RECORDS_UDF
These procedures are used when a FETCH BULK is done into a collection of records.
FETCH_BULK_COLLECTIONS_UDF
These procedures are used when the FETCH statement is done into one or multiple collections. Since the columns are specified in this FETCH operation, an override for specific COLUMN_NAMES is not necessary.
FETCH_BULK_RECORD_COLLECTIONS_UDF
These procedures are used when a FETCH BULK is done into a record of collections.
Last updated
Was this helpful?