Snowflake JS Proc - snowflake-cloud-data-platform

create or replace procedure test_09172(c_custkey varchar(25)
,c_mktsegment varchar(25)
,cname varchar(25)) returns string not null language javascript execute as owner as $$
var sqlquery="";
var fltConvUomPK="";
var fltConvFactorPK="";
var ParentClass="";
var VMAJOR="";
var VMINOR="";
try {
var sql_command =`SELECT C_ADDRESS,C_NATIONKEY
from customers
WHERE c_custkey=C_CUSTKEY and c_name=CNAME and C_MKTSEGMENT=C_MKTSEGMENT`;
var rs=snowflake.createStatement( {sqlText: sql_command});
var result_set1 = rs.execute();
}
catch(err)
{
return err.message
}
return rs.getSqlText(); $$;
While executing "call test_09172('537289','FURNITURE','Customer#000537289');"
I am getting below error.
JavaScript execution error: Uncaught TypeError: Cannot read property
'getSqlText' of undefined in TEST_09172 at ' return rs.getSqlText();'
position 14 stackstrace: TEST_09172 line: 28
Please help me on this to fix

The error seems related to an undefined object, but your code worked without any errors when I tried to reproduce it.
I noticed that you do not bind your parameters to your SQL:
var sql_command =`SELECT C_ADDRESS,C_NATIONKEY
from customers
WHERE c_custkey=C_CUSTKEY and c_name=CNAME and C_MKTSEGMENT=C_MKTSEGMENT`;
SQL is not case-sensitive, so you just compare the columns with themselves (c_custkey=C_CUSTKEY and C_MKTSEGMENT=C_MKTSEGMENT). c_name=CNAME will probably produce an error.
To avoid confusion between the column and parameter names, I rewrote the query:
create or replace procedure test_09172(c_custkey_p varchar(25)
,c_mktsegment_p varchar(25)
,c_name_p varchar(25)) returns string not null language javascript execute as owner as $$
var sqlquery="";
var fltConvUomPK="";
var fltConvFactorPK="";
var ParentClass="";
var VMAJOR="";
var VMINOR="";
try {
var sql_command =`SELECT C_ADDRESS,C_NATIONKEY
from customers
WHERE c_custkey=? and c_name=? and C_MKTSEGMENT=?`;
var rs=snowflake.createStatement( {sqlText: sql_command , binds:[ C_CUSTKEY_P, C_NAME_P , C_MKTSEGMENT_P ] });
}
catch(err)
{
return err.message
}
return rs.getSqlText();
$$;
On my tests, it works as expected but I don't have your data so you should test it.

The method getSqlText is available for the Statement object not ResultSet, see here:
getSqlText()
This method returns the text of the prepared query in the Statement object.

Related

Unable to execute multiple stored procedures in the parent stored procedure Snowflake

Iam new to Snowflake so this question might be very silly but iam unable to resolve this issue,
I have 2 child procs child1 and Child2 which are called from a Parent procedure, the issue is only one child proc is getting executed, whichever is called first and skips the other procedure.
create or replace procedure parent_prc(P_SRC_DB varchar,P_TGT_DB varchar)
returns varchar
language javascript
as
$$
var some_sql_cmd = "TRUNCATE TABLE "+P_SRC_DB+".schema_a.table1"
var some_sql_stmt = snowflake.createStatement( {sqlText: trunc_prestg_cmd} );
var child1_proc_exec_cmd = "call
"+P_TGT_DB+".schema_a.child1_proc('"+P_SRC_DB+"','"+P_TGT_DB+"')"
var child1_proc_stmt = snowflake.createStatement( {sqlText: child1_proc_exec_cmd} )
var child2_proc_exec_cmd = "call
"+P_TGT_DB+".schema_a.child2_proc('"+P_SRC_DB+"','"+P_TGT_DB+"')"
var child2_proc_stmt = snowflake.createStatement( {sqlText: child2_proc_exec_cmd} )
try
{
child1_proc_stmt.execute();
}
catch(err)
{
return(" Error :"+err);
}
try
{
child2_proc_stmt.execute();
}
catch(err)
{
return(" Error :"+err);
}
try
{
some_sql_stmt.execute();
}
catch(err)
{
return("Truncate Error :"+err);
}
$$
It executes Child1_proc and directly goes some_sql_stmt execution and Child2_proc is not executed, if i remove child1_proc, the Child2_proc is executed, iam not sure what is the issue, please help..
Can you confirm that the child1_proc is successful? You should also see an error in the parent_prc call. If you look in query history for the session id (use: select current_session(); to get session id), you can see all queries executed by the stored procedure and it should help identify the issue.
In the example provided above, the below part will have a syntax error, but this wouldn't allow the procedure to compile. (some_sql_cmd should be trunc_prestg_cmd)
var some_sql_cmd = "TRUNCATE TABLE "+P_SRC_DB+".schema_a.table1"
var some_sql_stmt = snowflake.createStatement( {sqlText: trunc_prestg_cmd}
);

How to bind JavaScript based date column in Snowflake SQL

I am creating snowflake JavaScript based store procedure. How can i refer the date data type variable in snowflake sql.
Here is the sample code:
In the below code ,please suggest how can i use 'dnblatestdt' variable in sql statement.
create or replace procedure test_proc_registration_master_perished_dt(PARAM_REG_SUB_UUID VARCHAR)
returns varchar not null
language javascript
as
$$
/*get latest ingestion_uuid for the given state*/
var step01=`select distinct dnb_applicable_dt,ingestion_uuid from temp_registration_hash_master `;
var statement01=snowflake.createStatement( {sqlText: step01,binds: [PARAM_REG_SUB_UUID]} );
variable1= statement01.execute();
variable1.next();
dnblatestdt=variable1.getColumnValue(1);
ingsuuid=variable1.getColumnValue(2);
/* check if the ingestion is successful or not*/
var step02=`select INGESTION_SUCCESSFUL from FILE_INGESTION_HISTORY where ingestion_uuid=:1 and date=:2::TIMESTAMP_LTZ::DATE`;
var statement02=snowflake.createStatement( {sqlText: step02,binds: [ingsuuid,dnblatestdt]} );
variable2= statement02.execute();
variable2.next();
ingsindc=variable2.getColumnValue(1);
return 'success'
$$
So I wrote a much simpler function that uses a similar pattern to your code:
create or replace procedure test_proc()
returns varchar not null
language javascript
as
$$
var step01 = `SELECT 6::number, '2022-01-27'::timestamp_ntz;`;
var statement01 = snowflake.createStatement( {sqlText: step01} );
results1 = statement01.execute();
results1.next();
ingsuuid = results1.getColumnValue(1);
dnblatestdt = results1.getColumnValue(2);
/* check if the ingestion is successful or not*/
var step02=`SELECT :1 * 2, DATEADD(year,-1, :2::timestamp_ntz);`;
var statement02 = snowflake.createStatement( {sqlText: step02,binds: [ingsuuid , dnblatestdt]} );
results2 = statement02.execute();
results2.next();
ingsindc = results2.getColumnValue(1);
return 'success'
$$
;
and using it works for me:
call test_proc();
TEST_PROC
success
I swapped the order of the reading parameters on the first function, but that should not be a problem.
this makes me thing your casting on the second instance is not working
:2::TIMESTAMP_LTZ::DATE
so I would suggest moving that casting to the first function, which you can test outside the stored procedure, thus.
SELECT DISTINCT dnb_applicable_dt::TIMESTAMP_LTZ::DATE, ingestion_uuid
FROM temp_registration_hash_master
when that is happy, you shouldn't need any casting on the second used of the values.

Getting same value for all records in snowflake

I have key_value table and key table. I have to load only key from key_value table and insert into key table using snowflake procedure. I have written below code. After executing it i am getting same key value in key table rather all the keys.
create or replace procedure proc_key_load()
returns varchar
language javascript
as
$$
var query=`select key from key_value`;
var ret=snowflake.createStatement( {sqlText: query}).execute();
var length=ret.getRowCount();
var counter=0;
while(counter<length){
ret.next();
var value=ret.getColumnValue(1);
var load_query=`insert into key_load values(` + value +`)`;
var ret=snowflake.createStatement( {sqlText: load_query}).execute();
counter += 1;
}
return 'SUCCESS';
$$
I didn't test it but as I see that you redefine "ret" in the loop. Try with assigning different variable:
create or replace procedure proc_key_load()
returns varchar
language javascript
as
$$
var query=`select key from key_value`;
var ret=snowflake.createStatement( {sqlText: query}).execute();
var length=ret.getRowCount();
var counter=0;
while(counter<length){
ret.next();
var value=ret.getColumnValue(1);
var load_query=`insert into key_load values(` + value +`)`;
var ret2 = snowflake.createStatement( {sqlText: load_query}).execute();
counter += 1;
}
return 'SUCCESS';
$$
Update: I tested the above code and it works. If you just need to copy some data from one table to another, you can just plain SQL right? Something like:
INSERT INTO key_load SELECT key FROM key_value;
Anyway, if you really need to use the above procedure, you can write it in a more efficient way:
create or replace procedure proc_key_load()
returns varchar
language javascript
as
$$
var query=`select key from key_value`;
var ret=snowflake.createStatement( {sqlText: query}).execute();
while(ret.next()){
var value=ret.getColumnValue(1);
var load_query=`insert into key_load values(?)`;
snowflake.createStatement( {sqlText: load_query, binds:[value] }).execute();
}
return 'SUCCESS';
$$;

Snowflake proc issue

I am having issue while executing below proc in snowflake.
Here is the code.
"**
create or replace procedure test_0916() returns string not null language javascript execute as owner as $$ try {
var sqlquery=" ";
var outcome=" ";
var x=" ";`
sql_command =select salary from EMPLOYEES where employee_id=8`;
var rs=snowflake.execute({sqlText:sql_command});
rs.next();/* Here not using while loop because it returns only one value*/
x=rs.getcolumnValue(1);
sqlquery=fltConvUomPK; }
catch (err) {
return "Error: " + err;
}"
return sqlquery;
$$;
**"
When I am trying to execute the proc as below
Blockquote
call test_0916();
I am getting below error.
Error: TypeError: rs.getcolumnValue is not a function
The method name is getColumnValue()
Can you check the spelling?
https://docs.snowflake.com/en/sql-reference/stored-procedures-api.html#getColumnValue

using variable while inserting data into a table - snowflake procedure

i am inserting a query from a variable into log table , but it is throwing error as below.
Failed: Code: 100183\n State: P0000\n Message: SQL compilation error:
syntax error line 3 at position 33 unexpected 'MM'.
syntax error line 7 at position 58 unexpected 'Current_Timestamp'
please refer INSERT_VOL2 where we are iserting values into a log table by using parameters. where v_WORK_SQL_ALRT_VOL2 is having the query , same query we are trying to insert into a log table. but it is throwing error.
below is the procedure code.
CREATE OR REPLACE PROCEDURE CDW_PROC.SAMPLE_PROCEDURE(col1 FLOAT, COL2 VARCHAR, COL3 VARCHAR, COL4 VARCHAR, COL5 VARCHAR, COL6 VARCHAR)
RETURNS VARCHAR(10000)
LANGUAGE JAVASCRIPT
STRICT
EXECUTE AS OWNER
AS
$$
try
{
var v_FILTER_ID=0;
var v_A_TYPE=COL2
var v_TYPE=COL3
var v_FILTER_ATTRIBUTE
var v_ORG=COL4;
var v_FILTER_CONDITION,v_FILTER_VALUE,v_FILTER_DESC;
var v_BRAND ='v_BRAND';
var v_F_TIME_CUR = 'v_F_TIME_CUR';
var v_F_TIME_PREV='v_F_TIME_PREV';
var v_F_RANK='v_F_RANK';
var v_F_TIME_BUCKET='v_F_TIME_BUCKET';
var v_CODE='v_CODE';
var v_ID=col1;
var v_TIME_FRAME=COL5;
var v_WK_MTH_FLG=COL6;
var SEL_SQL=snowflake.execute({sqlText: "SELECT ID,TYPE,ORG,SUB_TYPE,FILTER_ID,FILTER_DESC,FILTER_ATTRIBUTE,FILTER_CONDITION,FILTER_VALUE,TIME_FRAME,WK_MTH_FLG FROM CDW_DB.FCT_TABLE WHERE ID=? AND TYPE =? AND SUB_TYPE =? AND ORG=? AND TIME_FRAME=? AND WK_MTH_FLG =?",binds:[v_ID,v_A_TYPE, v_TYPE, v_ORG, v_TIME_FRAME, v_WK_MTH_FLG]});
while(SEL_SQL.next())
{
var v_ID=SEL_SQL.getColumnValue(1);
var v_A_TYPE=SEL_SQL.getColumnValue(2);
v_WORK_SQL_ALRT_VOL2 = `insert into CDW_US_DIMS_DB.PLANNED_CALL1
select DISTINCT FCT.PFZ_CUST_ID,
CAST(TO_CHAR(FCT.CALL_DATE_VOD ,'MM/DD/YYYY') AS VARCHAR(10)) AS PLANNED_CALL_DATE,
RANK() OVER (PARTITION BY FCT.PFZ_CUST_ID ORDER BY FCT.DT_SK ASC,FCT.CREATEDDATE ASC) AS RNK
from CDW_US_PROCESSING_VW.VVA_REP_PLANNED_CALLS FCT WHERE PFZ_CUST_ID <> -1
and FCT.${v_F_SALES_ORG_CODE}
and CALL_DATE_VOD > CURRENT_TIMESTAMP(0) QUALIFY RNK=1;`;
var v_WORK_SQL_EXEC=snowflake.createStatement({sqlText: v_WORK_SQL_ALRT_VOL2});
var VOL2_RESULT=v_WORK_SQL_EXEC.execute();
var INSERT_VOL2=snowflake.execute({sqlText: "INSERT INTO CDW_DB.log_tbl VALUES ("+v_ID+",'"+v_A_TYPE+"','"+v_WORK_SQL_ALRT_VOL2+"',Current_Timestamp)"});
var RESULT='Success';
return RESULT;
}
catch(err)
{
RESULT="Failed: Code: "+err.code+"\\n State: "+ err.state;
RESULT+="\\n Message: "+err.message;
RESULT+="\\n Stack Trace:\\n"+err.StackTraceTxt;
return RESULT;
}
$$
;
The pasted code has multiple errors that would prevent it from working, or even giving that specific error message:
A block needs to be closed before catch with a }.
The variable col1 should be COL1.
v_F_SALES_ORG_CODE is never defined, but used.
Once all that is fixed, everything works well until these lines:
var v_WORK_SQL_EXEC=snowflake.createStatement({sqlText: v_WORK_SQL_ALRT_VOL2});
var VOL2_RESULT=v_WORK_SQL_EXEC.execute();
But then we find this:
var INSERT_VOL2=snowflake.execute({sqlText: "INSERT INTO CDW_DB.log_tbl VALUES ("+v_ID+",'"+v_A_TYPE+"','"+v_WORK_SQL_ALRT_VOL2+"',Current_Timestamp)"});
The problem is that v_WORK_SQL_ALRT_VOL2 is a full sql query - and inserting a full SQL query string in the middle of an insert statement will simply not work.
This code needs a lot of cleaning and work, but at least we found where the two errors in the question are coming from.
i am able to insert that values now, after assigning current_timestamp values to a variable and that variable is using in Insert query. Thank you for your suggestions. grately appriciated.

Resources