How to bind JavaScript based date column in Snowflake SQL - snowflake-cloud-data-platform

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.

Related

Snowflake regexp_replace not working as expected

I tried to get paths enclosed by double quotes (ex: "path"."to"."element"). It also strips any bracket-enclosed array element references (like "[0]")
var path_name = "regexp_replace(regexp_replace("customers[0].name",'\\[(.+)\\]'),'(\\w+)','"\\1"')" ;
I tried this method but it is displaying error
So this is a really poorly written question. But lets play the guessing game anyways.
So you have a Javascript stored procedure, and you have that line it side it, and it doesn't work as you expect: lets guess it looks like:
create or replace procedure sp()
returns VARCHAR
language javascript
as
$$
var txt = '"customers[0].name"';
var sql_regexp1 = '\\\\[(.+)\\\\]';
var sql_regexp2 = '(\\\\w+)';
var sql_rep_2 = '\"\\\\1\"';
var full_rep1 = "regexp_replace('" + txt + "','"+ sql_regexp1 +"')";
var full_rep2 = "select regexp_replace(" + full_rep1 + ",'"+ sql_regexp2 +"','"+ sql_rep_2 + "');";
//return full_rep2;
var statement = snowflake.createStatement( {sqlText: full_rep2} );
var result_set1 = statement.execute();
result_set1.next()
return result_set1.getColumnValue(1);
$$;
;
and if you uncomment out the early return to can see the full_rep2
thus you can test that the inner SQL
select regexp_replace('"customers[0].name"','\\[(.+)\\]');
gives:
REGEXP_REPLACE('"CUSTOMERS[0].NAME"','\[(.+)\]')
"customers.name"
lets assume that's correct!
then you can check the outer replace:
select regexp_replace(regexp_replace('"customers[0].name"','\\[(.+)\\]'),'(\\w+)','"\\1"');
which gives:
REGEXP_REPLACE(REGEXP_REPLACE('"CUSTOMERS[0].NAME"','\[(.+)\]'),'(\W+)','"\1"')
""customers"."name""
and if we call the stored procedure:
call sp();
we get:
SP
""customers"."name""
So this was "how I debugged the SQL/Javascript" to have "valid working SQL. The question then becomes, what output did you want. And can you get there from here.

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 Syntax when passing array into insert query

My code obtains the column list of a table I have created
the attributes of this table are the contextual values of a session in snowflake
such as USER,DATABASE,WAREHOUSE...ETC
Afterwards it places those attribute names into an array
I then try to call on these names in making an insert query and this is where I am struggling with the syntax
Because each value in my array is USER, DATABASE,WAREHOUSE
I am trying to call on the context functions
like CURRENT_WAREHOUSE()
Can someone please help me with the syntax
for(i=0;i<arr.length;i++){
v_sqlCode = `INSERT INTO SESSION_ATTRIBUTES( arr[i] )
"VALUES ("CALL CURRENT_"+arr[i]+"()")';
}
You can't directly use the output of a Snowflake stored procedure programmatically. If you need to use the output value, you have to collect it using the RESULT_SCAN table function. That can get a bit complex to handle, directly in code, so it's far better to place it into a helper function. See this example of one SP calling another one and using its output value. It does this by calling the SP using the callSP helper function. Use the entire SQL statement including the call command into the SQL parameter for that function. It will run the SP, grab the result from the result_scan table function and return it.
create or replace procedure HELLO_WORLD()
returns string
language javascript
as
$$
return "Hello, world.";
$$;
create or replace procedure CALL_HELLO_WORLD()
returns string
language javascript
execute as caller
as
$$
return callSP(`call HELLO_WORLD()`);
// ---- Main function above, helper functions below.
function callSP(sql){
let cmd = {sqlText: sql};
let stmt = snowflake.createStatement(cmd);
stmt.execute();
let result_scan = `select $1 from table(result_scan(last_query_id()));`;
let result_cmd = {sqlText: result_scan};
let result_stmt = snowflake.createStatement(result_cmd);
let rs = stmt.execute();
if(rs.next()) {
return rs.getColumnValue(1);
} else {
return null;
}
}
$$;
call call_hello_world();

how to pass variant data into snowflake table using snowflake stored procedure

may i know how to pass variant data into snowflake table using snowflake stored procedure .
CREATE
OR REPLACE PROCEDURE abc(
MY_ID STRING,
P_FILTERS VARIANT
) RETURNS VARIANT
LANGUAGE JAVASCRIPT as $$
try
{
var P_FILTERS=P_FILTERS;
var query=" INSERT INTO abc (SQ_ID,id,\
FILTERS,\
ITERATION)\
VALUES (abc.nextval,\
:1,\
:2,\
0); "
var sql = snowflake.createStatement( {sqlText: query,binds:[MY_ID,P_FILTERS] });
var resultSet = sql.execute();
COMMIT;
}
catch(error)
{
return (error);
}
$$;
can some one help and undestand in letting me know
Thanks,
Nikhil
The solution isn't totally straightforward, but going from variant to string to variant solves the problem:
create or replace temp table variants as
select 'a'::string id, parse_json('{"hello":"world"}') v
;
CREATE
OR REPLACE PROCEDURE insert_variant(
MY_ID STRING,
P_FILTERS VARIANT
) RETURNS VARIANT
LANGUAGE JAVASCRIPT as $$
var query="INSERT INTO variants (id, v) select :1, parse_json(:2); "
var sql = snowflake.createStatement({
sqlText: query
, binds:[MY_ID, JSON.stringify(P_FILTERS)]
});
var resultSet = sql.execute();
$$;
call insert_variant('c', parse_json('{"hello2":"world3"}'))
;
This because
Currently, only JavaScript variables of type number, string, and SfDate can be bound. https://docs.snowflake.com/en/sql-reference/stored-procedures-usage.html#binding-variables
And trying to parse JSON on the VALUES() part of an insert gives you the error "Invalid expression in VALUES clause". But having an INSERT+SELECT solves it.

Use of Variable in Snowflake Stored Procedure

I have to add a variable MaxDate in my SQL Stored Proc (shown below). The code gets errored out since MaxDate is not represented by its value. Any idea on how I can pass a variable in a stored proc?
create or replace procedure Load_Employee()
returns varchar not null
language javascript
EXECUTE AS CALLER
as
$$
//Variable Initialization
var IntegrationTable ='EMPLOYEE';
var TypeID=0;
var MaxDate=' ';
var cmd = "Select max(COMPLETED_DATE) from SCHEMA.TABLE where TARGET_TABLE_NAME= " + "'" + IntegrationTable + "'" ;
var sql = snowflake.createStatement({sqlText: cmd});
var result = sql.execute();
result.next();
MaxDate=result.getColumnValue(1);
var cmd=` Insert into PersonTable
select SHA1(concat(Person_id,'|','Person')) ,12345678,SHA1(concat('Payroll','|','Pay','|', Load_Date)) ,current_timestamp() , Tenant
from Schema.PERSONTABLE where Date_Added >= MaxDate
where TYPE='ABC' ;`;
$$
;
If your query to get MaxDate works right, then the value should be in the variable. The problem is it's not being replaced in the sql variable defining the insert statement.
Since you're using backticks to open and close the string, you can use a special JavaScript notation to replace the variable with its value, ${MaxDate}.
Your definition of the insert statement would look like this:
var cmd=` Insert into PersonTable
select SHA1(concat(Person_id,'|','Person')) ,12345678,SHA1(concat('Payroll','|','Pay','|', Load_Date)) ,current_timestamp() , Tenant
from Schema.PERSONTABLE where Date_Added >= ${MaxDate}
where TYPE='ABC' ;`;
If that doesn't work, try cutting the SP short with return MaxDate; to see what got assigned to that variable. Also it's very helpful to check the query history view to see what SQL actually ran inside a stored procedure.
Also, I think this is the same SP that was having an issue with a null return. You'll need to return a string value using something like return 'Success'; or something to avoid getting an error for the null return. That's because of the returns varchar not null in the definition.

Resources