Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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';
$$

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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';
$$;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading