An array with the values or the variables to bind into the SQL statement.
opts (optional)
This is a Javascript object to describe how the values returned by the exec should be formated, this is used for SELECT statements.
Valid arguments for opts parameter
The following tables describe, how arguments should be sent to opts parameter in EXEC call:
Options when a query returns a single row
opts
description
{ }
When opts is empty or not sent to exec call, the data will be returned inside an array.
{vars: 0}
This has the same effect as the default option. It will return the data inside an array.
{vars: 1}
This is used when a query returns just one column and one row. EXEC will return the value directly. This is equivalent to EXEC(stmt)[0]
{rec:recordVariable}
Used when you want to store the values returned by the query inside a record. Translation of records is described in Records translation reference. Record variable should be passed as an argument.
{row: 1}
This option returns a copy of ResultSet, this means that the object returned contains the methods described in ResultSet Snowflake documentation.
Options when a query returns multiple rows
opts
Description
{row:2}
With this option, it always returns a copy of the ResultSet regardless of the number of rows returned by the EXEC.
For all the samples, SnowConvert helpers Code were removed. You can find them here.
The following code examples illustrates how EXEC works.
EXEC simple case
Oracle
IN -> Oracle_01.sql
--Additional Params: -t JavaScriptCREATEORREPLACEPROCEDURE EXECUTE_PROC ASBEGIN--CREATES HARDWARE TABLE WITH COLUMNS ID, DEVICE AND COLOR--THIS IS AN EXECUTE IMMEDIATE JUST WITH AN STATEMENTEXECUTEIMMEDIATE'CREATE TABLE HARDWARE (ID NUMBER, DEVICE VARCHAR2(15), COLOR VARCHAR(15))';END;
Snowflake
OUT -> Oracle_01.sql
CREATEORREPLACEPROCEDURE EXECUTE_PROC ()RETURNS STRINGLANGUAGE JAVASCRIPTCOMMENT ='{"origin":"sf_sc","name":"snowconvert","version":{"major":1, "minor":0},{"attributes":{"component":"oracle"}}'EXECUTEASCALLERAS$$// SnowConvert Helpers Code section is omitted.//CREATES HARDWARE TABLEWITH COLUMNS ID, DEVICE AND COLOR//THIS IS AN EXECUTEIMMEDIATE JUST WITH AN STATEMENTEXEC(`CREATE OR REPLACE TABLE HARDWARE (ID NUMBER(38, 18), DEVICE VARCHAR(15), COLOR VARCHAR(15))`);$$;
EXEC with bindings
Oracle
IN -> Oracle_02.sql
--Additional Params: -t JavaScriptCREATEORREPLACEPROCEDURE EXECUTE_PROC AS ID_VAR NUMBER; DEVICE_VAR VARCHAR2(15); DEV_COLOR VARCHAR2(15); COLOR_VAR VARCHAR2(15);BEGIN--EXEC WITH BINDINGS--INSERTS A ROW WITH | 12 | MOUSE | BLACK | VALUES USING DIRECT BINDING FOR MOUSEEXECUTEIMMEDIATE'INSERT INTO HARDWARE VALUES (12, :MOUSE, ''BLACK'')'USING'MOUSE';--INSERTS A ROW WITH | 13 | KEYBOARD | WHITE | VALUES USING DIRECT BINDING FOR 13 AND KEYBOARDEXECUTEIMMEDIATE'INSERT INTO HARDWARE VALUES (:ID, :KEYBOARD, ''WHITE'')'USING13, 'KEYBOARD';--INSERTS A ROW WITH | 14 | HEADSET | GRAY | VALUES USING BINDING VARIABLES ID_VAR :=14; DEVICE_VAR :='HEADSET'; COLOR_VAR :='GRAY';EXECUTEIMMEDIATE'INSERT INTO HARDWARE VALUES (:DEV_ID, :DEV_VAR, :DEV_COLOR)'USING ID_VAR, DEVICE_VAR, COLOR_VAR;END;
Snowflake
OUT -> Oracle_02.sql
CREATEORREPLACEPROCEDURE EXECUTE_PROC ()RETURNS STRINGLANGUAGE JAVASCRIPTCOMMENT ='{"origin":"sf_sc","name":"snowconvert","version":{"major":1, "minor":0},{"attributes":{"component":"oracle"}}'EXECUTEASCALLERAS$$// SnowConvert Helpers Code section is omitted. let ID_VAR; let DEVICE_VAR; let DEV_COLOR; let COLOR_VAR;//EXECWITH BINDINGS//INSERTS A ROWWITH | 12 | MOUSE | BLACK | VALUESUSING DIRECT BINDINGFOR MOUSEEXEC(`INSERT INTO HARDWAREVALUES (12, ?, 'BLACK')`,[`MOUSE`]);//INSERTS A ROWWITH | 13 | KEYBOARD | WHITE | VALUESUSING DIRECT BINDINGFOR13AND KEYBOARDEXEC(`INSERT INTO HARDWAREVALUES (?, ?, 'WHITE')`,[13,`KEYBOARD`]);//INSERTS A ROWWITH | 14 | HEADSET | GRAY | VALUESUSINGBINDING VARIABLES ID_VAR =14; DEVICE_VAR =`HEADSET`; COLOR_VAR =`GRAY`;EXEC(`INSERT INTO HARDWAREVALUES (?, ?, ?)`,[ID_VAR,DEVICE_VAR,COLOR_VAR]);$$;
EXEC with options
Oracle
IN -> Oracle_03.sql
--Additional Params: -t JavaScriptCREATEORREPLACEPROCEDURE EXECUTE_PROC ASBEGIN--STORES THE ID INTO ID_VAREXECUTEIMMEDIATE'SELECT ID FROM HARDWARE WHERE COLOR = ''BLACK'''INTO ID_VAR; DBMS_OUTPUT.PUT_LINE(ID_VAR);--STORES THE ID AND DEVICE INTO ID_VAR AND DEV_VAR, USING BINDING FOR COLOR COLOR_VAR :='BLACK';EXECUTEIMMEDIATE'SELECT ID, DEVICE FROM HARDWARE WHERE COLOR = :DEV_COLOR'INTO ID_VAR, DEVICE_VAR USING COLOR_VAR; DBMS_OUTPUT.PUT_LINE(ID_VAR ||' '|| DEVICE_VAR);END;
Snowflake
OUT -> Oracle_03.sql
CREATEORREPLACEPROCEDURE EXECUTE_PROC ()RETURNS STRINGLANGUAGE JAVASCRIPTCOMMENT ='{"origin":"sf_sc","name":"snowconvert","version":{"major":1, "minor":0},{"attributes":{"component":"oracle"}}'EXECUTEASCALLERAS$$// SnowConvert Helpers Code section is omitted.//STORES THE ID INTO ID_VAR [ID_VAR] =EXEC(`SELECT ID FROM HARDWAREWHERE COLOR = 'BLACK'`);EXEC(`!!!RESOLVE EWI!!! /*** SSC-EWI-OR0115 - CHECK UDF IMPLEMENTATION FOR DBMS_OUTPUT.PUT_LINE. ***/!!!CALL DBMS_OUTPUT.PUT_LINE_UDF(ID_VAR)`);//STORES THE ID AND DEVICE INTO ID_VAR AND DEV_VAR, USINGBINDINGFOR COLOR COLOR_VAR =`BLACK`; [ID_VAR,DEVICE_VAR] =EXEC(`SELECT ID, DEVICE FROM HARDWAREWHERE COLOR = ?`,[ !!!RESOLVE EWI!!! /*** SSC-EWI-0053 - OBJECT COLOR_VAR MAY NOT WORK PROPERLY, ITS DATATYPE WAS NOT RECOGNIZED ***/!!! COLOR_VAR]);EXEC(`!!!RESOLVE EWI!!! /*** SSC-EWI-OR0115 - CHECK UDF IMPLEMENTATION FOR DBMS_OUTPUT.PUT_LINE. ***/!!!CALL DBMS_OUTPUT.PUT_LINE_UDF(NVL(ID_VAR :: STRING, '') || ' ' || NVL(DEVICE_VAR :: STRING, ''))`);$$;```
For the following sample, EXEC call returns [12], with object destructuring ID_VAR stores 12:
[ID_VAR] =EXEC(`SELECT ID FROM PUBLIC.HARDWARE WHERE COLOR = 'BLACK'`);
The following two EXEC calls are alternative ways for the previous sample without object destructuring:
ID_VAR =EXEC(`SELECT ID FROM PUBLIC.HARDWARE WHERE COLOR = 'BLACK'`)[0];ID_VAR =EXEC(`SELECT ID FROM PUBLIC.HARDWARE WHERE COLOR = 'BLACK'`, {vars:1});
Object destructuring also works with bindings as you may note on these statements (EXEC call returns [12, "MOUSE"] values):
COLOR_VAR =`BLACK`;[ID_VAR,DEVICE_VAR] =EXEC(`SELECT ID, DEVICE FROM PUBLIC.HARDWARE WHERE COLOR = ?`,[COLOR_VAR]);
To obtain the actual result set returned by Snowflake, you can use this synaxis:
let RESULT_SET_COPY;RESULT_SET_COPY =EXEC(`SELECT * FROM PUBLIC.HARDWARE WHERE COLOR = 'BLACK'`, {row:1});/* RETURNS{ "COLOR": "BLACK", "DEVICE": "MOUSE", "ID": 12, "getColumnCount": {}, ... "next": {}}*/
--Additional Params: -t JavaScriptCREATEORREPLACEPROCEDURE EXECUTE_PROC ASTYPE DEVTRECTYP IS RECORD ( ID NUMBER(4) NOT NULL :=0, DEV_TYPE VARCHAR2(30) NOT NULL :='UNKNOWN', COLOR VARCHAR2(30) :='GREEN' ); DEV_VARIABLE DEVTRECTYP; BEGIN--STORES THE ROW VALUES IN THE RECORDEXECUTEIMMEDIATE'SELECT * FROM HARDWARE WHERE COLOR = ''BLACK'''INTO DEV_VARIABLE; DBMS_OUTPUT.PUT_LINE(DEV_VARIABLE.ID ||' '|| DEV_VARIABLE.DEV_TYPE ||' '|| DEV_VARIABLE.COLOR);END;
Snowflake
OUT -> Oracle_04.sql
CREATEORREPLACEPROCEDURE EXECUTE_PROC ()RETURNS STRINGLANGUAGE JAVASCRIPTCOMMENT ='{"origin":"sf_sc","name":"snowconvert","version":{"major":1, "minor":0},{"attributes":{"component":"oracle"}}'EXECUTEASCALLERAS$$// SnowConvert Helpers Code section is omitted. class DEVTRECTYP { ID =0 DEV_TYPE =`UNKNOWN` COLOR =`GREEN` constructor() { [...arguments].map((element,Index) => this[(Object.keys(this))[Index]] = element) } } let DEV_VARIABLE = new DEVTRECTYP();//STORES THE ROWVALUESIN THE RECORDEXEC(`SELECT * FROM HARDWAREWHERE COLOR = 'BLACK'`,{ rec : DEV_VARIABLE });EXEC(`!!!RESOLVE EWI!!! /*** SSC-EWI-OR0115 - CHECK UDF IMPLEMENTATION FOR DBMS_OUTPUT.PUT_LINE. ***/!!!CALL DBMS_OUTPUT.PUT_LINE_UDF(NVL(? :: STRING, '') || ' ' || NVL(? :: STRING, '') || ' ' || NVL(? :: STRING, ''))`,[DEV_VARIABLE.ID,DEV_VARIABLE.DEV_TYPE,DEV_VARIABLE.COLOR]);$$;
This is still a work in progress. The transformation to properly store the record values will be:
EXEC(`SELECT * FROM PUBLIC.HARDWARE WHERE COLOR = 'BLACK'`, {rec:DEV_VARIABLE});