在Snowdlake dwh中是否可以检索SQL查询结果并将其作为变量传递给同一过程中的下一个查询?

我正尝试尝试%ROWCOUNT,CURSOR大小写,并陷入以下困境:

CREATE OR REPLACE PROCEDURE PASS_VAR_IN_JS()
RETURNS VARCHAR
LANGUAGE javascript
  AS
    $$
    var command = "SELECT * FROM TABLE WHERE ATTRIBUTE = 'Value'";
    var stmt = snowflake.createStatement( {sqlText: command} );
    var result1 = stmt.execute();
    return result1.getColumnName(3); //this returns column name
    var col_name = result1.getColumnName(3); //save column name to variable

    //pass the column name to the query
    var stmt = snowflake.createStatement({sqlText: "SELECT * FROM INFORMATION_SCHEMA WHERE COLUMN_NAME = ;", binds:[col_name]});

    $$;
UPDATE - working code that passes one result to another query
<pre>
CREATE OR REPLACE PROCEDURE szpotma_rowcount()
RETURNS VARCHAR(250)
LANGUAGE JAVASCRIPT
as
$$
  var command = `SELECT ATTR1 FROM TABLE1 LIMIT 1`;
  var stmt = snowflake.createStatement( {sqlText: command} );
  var rs = stmt.execute(), col_name ;

  if (rs.next()) {
    col_name = rs.getColumnValue(1);
    stmt = snowflake.createStatement({
        sqlText: `SELECT STATUS FROM TABLE_STATUS WHERE ATTR1 = :1`, binds: [col_name]});

    //view second query results
    rs = stmt.execute();
    rs.next()
    return rs.getColumnValue(1);
  }
$$

最佳答案

当然。

您可以执行以下操作:

  var command = `SELECT * FROM TABLE WHERE ATTRIBUTE = 'Value'`;
  var stmt = snowflake.createStatement( {sqlText: command} );
  var rs = stmt.execute(), col_name ;
  if (rs.next()) {
    col_name = rs.getColumnValue(3);

    stmt = snowflake.createStatement({
        sqlText: `SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME = :1`,
          binds: [col_name]
    });
  }


我列出了多种从结果集中提取数据的方法:
How to use Show Command in Stored Procedures - Not working

[从上方复制]:

如果您有多列,则代码会变得稍微复杂一些:

  var result = [], row, col;
  while (rs.next()) {
    row = [];
    for (col = 1; col <= stmt.columnCount; col++)
      row.push(rs.getColumnValue(col));
    result.push(row);
  }


核心JavaScript程序员可能会将其压缩为:

var result = [], cols = Array.from({ length: stmt.columnCount }, (v, i) => i + 1);
while (rs.next()) result.push(cols.map(c => rs.getColumnValue(c)));


最后一个变体,其中第一个结果行包含列名,随后的行包含结果集中的数据,可以作为result [row] [column]访问:

var result =
    [ Array.from({ length: stmt.columnCount }, (v, i) => stmt.getColumnName(i + 1)) ];
while (rs.next()) result.push(result[0].map(cn => rs.getColumnValue(cn)));

09-16 14:04