Javascript 雪花程序中的数组

Javascript Array in snowflake procedure

我有一个雪花过程,它使用传递给雪花过程的参数创建一个 table,如下所示:

CREATE OR REPLACE PROCEDURE SP_test (c_names varchar)
RETURNS STRING
LANGUAGE JAVASCRIPT
AS $$
  
  var results_array = [];
  var c_names_combined = "'" + C_NAMES.split(',').join("','") + "'"
  
  sql_command = `SELECT TABLE_CATALOG||'.'||TABLE_SCHEMA||'.'||TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='MAP_MEMBER_PLATFORM'`
  sql_command = sql_command + ` AND TABLE_SCHEMA IN (` + c_names_combined +`)`
  sqlStmt = snowflake.createStatement( {sqlText: sql_command} );
  result_sqlStmt  = sqlStmt.execute();
  
  while (result_sqlStmt.next()) {
    var full_table_names = result_sqlStmt.getColumnValue(1);
    results_array.push([full_table_names]);
  }
  var c_1 = results_array[0];
  var c_2 = results_array[1];
  var c_3 = results_array[2];

  
  sql_command = `CREATE OR REPLACE TABLE table_to_create
                          AS
                          SELECT * FROM ` + c_1 +`
                           UNION ALL
                          SELECT * FROM ` + c_2 +`
                           UNION ALL
                          SELECT * FROM ` + c_3 +``
  sqlStmt = snowflake.createStatement( {sqlText: sql_command} );
  result_sqlStmt  = sqlStmt.execute();
  
  result = "done"
  return result
  
$$
; 
call SP_test('A,B,C');

如果我传递三个参数 A、B 和 C,代码工作正常。但我希望它对传递的参数尽可能多。

例如,如果我使用参数 ('A, B, C, D, E') 调用该过程。我希望我的 table(table_to_create) 使用我传递的参数创建一个 table。(在这种情况下:A、B、C、D、E)

请帮忙!

像这样的东西应该有用,可能不理想,但应该有用:

var results_array = [1, 2, 3, 4, 5];
var sql_command="CREATE OR REPLACE TABLE table_to_create AS ";
for (let i = 0; i < results_array.length; i++) {
   sql_command += "SELECT * FROM " + results_array[i] + " UNION ALL ";
 }

var last_words = sql_command.split(" ")
last_words.pop();
last_words.pop();
last_words.pop();
var sql_command_new = last_words.join(" ");

运行 在节点控制台上我得到:

> console.log(sql_command_new);
CREATE OR REPLACE TABLE table_to_create AS SELECT * FROM 1 UNION ALL SELECT * FROM 2 UNION ALL SELECT * FROM 3 UNION ALL SELECT * FROM 4 UNION ALL SELECT * FROM 5

删除最后一个 space 和最后一个 UNION ALL 需要 3 个 pop。 通过这种方式,您可以根据需要添加任意数量的参数。