This section describes the usage of different functions to achieve functional equivalence for Teradata cursors in JavaScript.
The cursor helper is a function that contains the main four actions that Teradata cursors perform such as Open, Fetch, Next, and Close.
CURSOR(), themain routine which declares the needed variables and other sub-routines.
OPEN(), opens the cursor executing the given statement, and updates the necessary variables.
NEXT(), moves the cursor to the next row (if any) of the statement and sets every column value to the current row.
FETCH(), obtains the values (if any) from the response of the statement executed.
CLOSE(), removes the temporary table from the _OUTQUERIES (if it was added in the EXEC helper) and unsets the necessary variables.
Cursor Sample Usage
Teradata
Replace procedure procedure1() dynamic result sets 2begin-------- Local variables --------declare sql_cmd varchar(20000) default' '; declare num_cols integer;------- Declare cursor withreturn only-------declare resultset cursor withreturn only for firststatement;------- Declare cursor -------declare cur2 cursor for select count(columnname) from table1;-------- Set -------- set sql_cmd='sel * from table1';-------- Prepare cursor -------- prepare firststatement from sql_cmd; -------- Open cursors -------- open resultset; open cur1;-------- Fetch ------------- fetch cur1 into val1, val2;-------- Close cursor -------- close cur1;end;
Snowflake output
CREATEORREPLACEPROCEDUREPUBLIC.procedure1 ()RETURNSSTRINGLANGUAGEJAVASCRIPTEXECUTEASCALLERAS$$// REGION SnowConvert Helpers Code//------ Local variables --------varSQL_CMD=` `;varNUM_COLS;varRESULTSET=newCURSOR(() =>FIRSTSTATEMENT,[],true);//----- Declare cursor -------varCUR2=newCURSOR(`SELECT count(columnname) FROM PUBLIC.table1`,[],false);//------ Set --------SQL_CMD=`SELECT * FROM PUBLIC.table1`;//------ Prepare cursor --------varFIRSTSTATEMENT=SQL_CMD;//------ Open cursors --------RESULTSET.OPEN();CUR1.OPEN();//------ Fetch -------------CUR1.FETCH() && ([val1,val2] =CUR1.INTO());//------ Close cursor --------CUR1.CLOSE();returnPROCRESULTS();$$;