Snowflake存储过程-如何检索CTAS语句创建的行数

时间:2019-12-16 16:33:41

标签: snowflake-data-warehouse

在雪花存储过程中,我正在执行CTAS语句,并希望检索结果对象中的行数。我们无权访问QUERY_HISTORY(我们得到一个错误),而RESULT_SCAN(LAST_QUERY_ID())也无济于事(它使我们获得了表xyz创建的结果,但没有元数据,即已创建的行数) )。

我可以在单独的查询中使用Select Count(*)来做到这一点,但这似乎是一个hack,因为“行数”就在“历史记录”中。

    CREATE OR REPLACE PROCEDURE EDW_ADMIN.DAG_TEST()
    RETURNS VARCHAR(512)
    LANGUAGE JAVASCRIPT
    AS 
    $$
    {
    let strCTAS = "";
    let rsCTAS;
    let rsRowsAffected;
    let rowsAffected = 0;   

    strCTAS = "CREATE OR REPLACE TABLE EDW_ADMIN.DEMO_PROC_TEMP AS SELECT * FROM RAW_BIR.H_RPTUNIT;";
    rsCTAS = snowflake.execute( {sqlText: strCTAS} );

    // This works in a Query Worksheet in the browser, but gives me the following error when called from a procedure
    //      "[Stored procedure execution error: Requested information on the current user is not accessible in stored procedure.]"
    rsRowsAffected = snowflake.execute( {sqlText: "SELECT ROWS_PRODUCED FROM TABLE(INFORMATION_SCHEMA.QUERY_HISTORY(RESULT_LIMIT=>100)) WHERE QUERY_ID = LAST_QUERY_ID();"} );
    rsRowsAffected.next();
    rowsAffected = rsRowsAffected.getColumnValue(1); 


    // This works, but you are doing execute i/o which is really un-necessary
    // rsRowsAffected = snowflake.execute( {sqlText: "SELECT COUNT(*) FROM EDW_ADMIN.DEMO_PROC_TEMP;"} );
    // rsRowsAffected.next();
    // rowsAffected = rsRowsAffected.getColumnValue(1); 

    // This does NOT work, RESULT_SCAN has no metadata associated with it, this returns "Table DEMO_PROC_TEMP successfully created."
    // rsRowsAffected = snowflake.execute ( {sqlText: "SELECT * FROM TABLE(RESULT_SCAN(LAST_QUERY_ID()));" } );
    // rsRowsAffected.next();
    // rowsAffected = rsRowsAffected.getColumnValue(1); 

    return rowsAffected;
}
$$
;

CALL EDW_ADMIN.DAG_TEST();

DROP EDW_ADMIN.DEMO_PROC_TEMP;
DROP PROCEDURE EDW_ADMIN.DAG_TEST();

1 个答案:

答案 0 :(得分:1)

尝试将作为调用者执行添加到存储过程声明中。例如:

create or replace procedure p() 
returns text
language javascript
execute as caller
as
  $$
  const stmt1 = snowflake.createStatement( { sqlText: "create or replace table t as select $1 x from values (1),(2),(3)" } )
  const rs1 = stmt1.execute()

  const stmt2 = snowflake.createStatement( { sqlText: "SELECT ROWS_PRODUCED FROM TABLE(INFORMATION_SCHEMA.QUERY_HISTORY(RESULT_LIMIT=>100)) WHERE QUERY_ID = LAST_QUERY_ID()" } )
  const rs2 = stmt2.execute()
  rs2.next()
  const rowsAffected = rs2.getColumnValue(1)
  return rowsAffected
  $$
;


call p();

返回3