雪花中存储的proc中是否有打印命令

时间:2020-05-18 17:24:40

标签: snowflake-cloud-data-platform

我在动态SQL中有一个proc引发错误。我需要查看proc正在运行的sql的打印语句。来自雪花的错误消息不是很有帮助。

这是我拥有的存储过程

CREATE OR REPLACE PROCEDURE util.dbo.Restore_db_ver("source_db" varchar, "restore_db" varchar, "dest_db" varchar)
returns float
LANGUAGE JAVASCRIPT
AS
$$
    var perm_cmd = "select cp_object,cp_object_name,cp_permission_type, cp_permission,cp_role from util.dbo.restore_perm where cp_database_name = '"+restore_db+"'"
    var perm_cmd_stmt = snowflake.createStatement({sqlText: perm_cmd})
    var perm_record = perm_cmd_stmt.execute()
    while(perm_record.next())
    {var perm_object = perm_record.getColumnValue(1)
     var perm_object_name = perm_record.getColumnValue(2)
     var perm_permission_type = perm_record.getColumnValue(3)
     var perm_permission_name = perm_record.getColumnValue(4)
     var perm_role_name = perm_record.getColumnValue(5)    


    var sql_cmd1 = "CREATE OR REPLACE DATABASE "+restore_db+" CLONE "+source_db+" AT (OFFSET => -1800)"
    var statement1 = snowflake.createStatement( {sqlText: sql_cmd1} )
    var result_set1 = statement1.execute()

    var sql_cmd2 = perm_permission_type+" "+perm_permission_name+" ON "+perm_object+" "+perm_object_name+" TO ROLE "+perm_role_name
    var statement2 = snowflake.createStatement( {sqlText: sql_cmd2} )
    var result_set2 = statement2.execute()
    }

$$

这引发了错误 JavaScript执行错误:未捕获ReferenceError:RESTORE_DB_VER中未在'var sql_cmd2 = perm_permission_type +“” + permission_name +“ ON” + perm_object +“” ++ perm_object_name +“ TO ROLE” + perm_role_name“位置44堆栈跟踪:RESTORE_p_VER行中未定义权限名>

是否有可用于输出sql的打印命令?

2 个答案:

答案 0 :(得分:0)

您的几行不以分号结尾,包括紧邻您的Permission_name变量定义的那一行。没有分号,它看起来像是JavaScript的代码行,因此Permission_name永远不会定义为变量。您也不应该在循环内部重新定义变量。

没有数据我无法测试代码,但是它看起来应该更像这样:

CREATE OR REPLACE PROCEDURE util.dbo.Restore_db_ver("source_db" varchar, "restore_db" varchar, "dest_db" varchar)
returns float
LANGUAGE JAVASCRIPT
AS
$$
    var perm_object, perm_object_name, perm_permission_type, perm_permission_name, perm_role_name, sql_cmd1, sql_cmd2, statement1, statement2
        result_set1, result_set2;
    var perm_cmd = "select cp_object,cp_object_name,cp_permission_type, cp_permission,cp_role from util.dbo.restore_perm where cp_database_name = '"+restore_db+"'";
    var perm_cmd_stmt = snowflake.createStatement({sqlText: perm_cmd});
    var perm_record = perm_cmd_stmt.execute();
    while(perm_record.next()){
        perm_object = perm_record.getColumnValue(1);
        perm_object_name = perm_record.getColumnValue(2);
        perm_permission_type = perm_record.getColumnValue(3);
        perm_permission_name = perm_record.getColumnValue(4);
        perm_role_name = perm_record.getColumnValue(5);    

        sql_cmd1 = "CREATE OR REPLACE DATABASE "+restore_db+" CLONE "+source_db+" AT (OFFSET => -1800)";
        statement1 = snowflake.createStatement( {sqlText: sql_cmd1} );
        result_set1 = statement1.execute();

        sql_cmd2 = perm_permission_type+" "+perm_permission_name+" ON "+perm_object+" "+perm_object_name+" TO ROLE "+perm_role_name;
        statement2 = snowflake.createStatement( {sqlText: sql_cmd2} );
        result_set2 = statement2.execute();
    }

$$

答案 1 :(得分:0)

选项1:
要查看运行前已执行的SQL命令,可以执行以下操作。

CREATE OR REPLACE PROCEDURE util.dbo.Restore_db_ver("source_db" varchar, "restore_db" varchar, "dest_db" varchar)
returns string
LANGUAGE JAVASCRIPT
AS
$$
    var perm_cmd = "select cp_object,cp_object_name,cp_permission_type, cp_permission,cp_role from util.dbo.restore_perm where cp_database_name = '"+restore_db+"'";

return perm_cmd ;
$$
call <procedure> < params> ;
SELECT *
FROM TABLE(result_scan(last_query_id()));

选项2: 要查看执行了什么,可以利用雪花查询标记和查询历史记录。

-- before executing the procedure
ALTER session SET QUERY_TAG = <procedure tag>;

使用过程标签过滤查询历史记录,以列出所有已执行的SQL命令。