如何在snowflake中使用JavaScript中的for循环遍历表并在相应的表中插入记录?

e3bfsja2  于 2024-01-05  发布在  Java
关注(0)|答案(2)|浏览(95)

不知何故,我的代码只插入行1和列1,2,3记录到输出表。不幸的是,它不插入记录行2向前。有人可以请帮助吗?
unfortely我的代码只插入第一行的列只.它不是通过remaing行迭代得到corrsponding coulmns插入到输出表.代码如下:

create or replace procedure proc3()
returns varchar
language javascript
as
$$
var query1= 'select \* from test2'
var query1_s = snowflake.createStatement({sqlText: query1});
var query1_s_res = query1_s.execute();
for (i = 1; i \<= query1_s_res.getRowCount(); i++){
query1_s_res.next()
X=  query1_s_res.getColumnValue(1)
Y = query1_s_res.getColumnValue(2)
Z = query1_s_res.getColumnValue(3)
var sql_source_table =
"INSERT INTO test3 VALUES (?,?,?)";
var source_max_day = snowflake.execute ({sqlText: sql_source_table, binds: \[X,Y,Z\]});  
return "Done";}
$$
;
call proc3()

字符串

bgibtngc

bgibtngc1#

你已经在for循环的最后定义了return语句。这是唯一一次的错误。把它删除,放到循环的外面。

flvtvl50

flvtvl502#

Try this:
create or replace procedure proc3()
returns varchar
language javascript
as
$$
var query1 = 'select * from test2';
var query1_s = snowflake.createStatement({sqlText: query1});
var query1_s_res = query1_s.execute();

while (query1_s_res.next()) {
    var X = query1_s_res.getColumnValue(1);
    var Y = query1_s_res.getColumnValue(2);
    var Z = query1_s_res.getColumnValue(3);
    
    var sql_source_table = "INSERT INTO test3 VALUES (?,?,?)";
    var source_max_day = snowflake.execute ({sqlText: sql_source_table, binds: [X, Y, Z]});
}

return "Done";
$$
;

字符串

相关问题