在存储过程中打印雪花查询

时间:2019-12-02 18:50:29

标签: snowflake-data-warehouse

我正在尝试使用Snowflake存储过程,并希望将以下存储过程中的SQL语句打印到屏幕上。

CREATE OR REPLACE PROCEDURE read_western_provinces()
  RETURNS VARCHAR NOT NULL
  LANGUAGE JAVASCRIPT
  AS
  $$
  var return_value = "";
  try {
      var command = "SELECT * FROM western_provinces ORDER BY province;"
      var stmt = snowflake.createStatement( {sqlText: command } );
      var rs = stmt.execute();
      if (rs.next())  {
          return_value += rs.getColumnValue(1);
          return_value += ", " + rs.getColumnValue(2);
          }
      while (rs.next())  {
          return_value += "\n";
          return_value += rs.getColumnValue(1);
          return_value += ", " + rs.getColumnValue(2);
          }
      }
  catch (err)  {
      result =  "Failed: Code: " + err.code + "\n  State: " + err.state;
      result += "\n  Message: " + err.message;
      result += "\nStack Trace:\n" + err.stackTraceTxt;
      }
  return return_value;
  $$
  ;

无论何时执行存储过程,如何将命令变量的值(包含SQL)打印到屏幕上?

谢谢

3 个答案:

答案 0 :(得分:3)

我还没有找到一种方法,但是至少有一些选择可以关闭。

  1. 您可以将输出插入表中。
  2. 您可以返回变量,而可以调试数组或类似形式的输出

    return { "output":return_value, "debug":result }
    
  3. 使用一个过程复制相似的内容。参见雪花社区的类似文章:https://community.snowflake.com/s/question/0D50Z00009Uu3cTSAR/debugging-procedures-technicques-print-std-out-etc

答案 1 :(得分:0)

使用getSqlText()以文本形式返回一条语句,请参见docs

将return语句从下面添加到过程的末尾,以使其在调用时打印该语句。

CREATE OR REPLACE PROCEDURE read_western_provinces()
  RETURNS VARCHAR NOT NULL
  LANGUAGE JAVASCRIPT
  AS
  $$
  var return_value = "";
  try {
      var command = "SELECT * FROM western_provinces ORDER BY province;"
      var stmt = snowflake.createStatement( {sqlText: command } );
      var rs = stmt.execute();
      if (rs.next())  {
          return_value += rs.getColumnValue(1);
          return_value += ", " + rs.getColumnValue(2);
          }
      while (rs.next())  {
          return_value += "\n";
          return_value += rs.getColumnValue(1);
          return_value += ", " + rs.getColumnValue(2);
          }
      }
  catch (err)  {
      result =  "Failed: Code: " + err.code + "\n  State: " + err.state;
      result += "\n  Message: " + err.message;
      result += "\nStack Trace:\n" + err.stackTraceTxt;
      }
  return stmt.getSqlText(); -- < will print/return the statement text
  $$
  ;

答案 2 :(得分:0)

在该过程之后只需使用调用方法即可。

1.CALL read_western_provinces('数据库名称','模式名称');

语法

CALL('database_name','schema_name');