【问题标题】:Execution error in store procedure SQL compilation error: invalid identifier 'TEST3' At Statement.execute存储过程 SQL 编译错误中的执行错误:Statement.execute 中的标识符“TEST3”无效
【发布时间】:2020-11-18 11:16:19
【问题描述】:

我在下面创建了存储过程,但是在 where 子句中解析 VAL 时出错,我也尝试了双引号 " 和 backstick `

create or replace TABLE T1 (
    COL VARCHAR(16777216)
);

insert into t1 values  ('test1');
insert into t1 values  ('test2');
insert into t1 values  ('test3');

create or replace procedure ifcopied(val varchar)
                                   returns varchar
                                   language javascript
                                   execute as caller
                                   as
                                   $$
                                   sql_command = "select * from t1 where col = " + VAL;
                                   var stmt = snowflake.createStatement({sqlText:sql_command});                       
                                   var res = stmt.execute();
                                   res.next();
                                   row_status = res.getColumnValue(1);
                                   return row_status                                   
                                   $$;

call ifcopied('test3')

;

遇到错误

Execution error in store procedure IFCOPIED: SQL compilation error: error line 1 at position 29 invalid identifier 'TEST3' At Statement.execute, line 4 position 50

存储过程 IFCOPIED 中的执行错误:SQL 编译错误:错误第 1 行在位置 29 无效标识符“TEST3”在 Statement.execute 第 4 行位置 50

【问题讨论】:

    标签: snowflake-cloud-data-platform


    【解决方案1】:

    除了 Gokhan 所拥有的之外,您还可以通过这种方式执行它。

    sql_command = `select * from t1 where col = '${VAL}';`
    

    这可以减少您对串联的使用,并允许您查看行中的变量。

    【讨论】:

      【解决方案2】:

      需要加单引号或者使用绑定变量:

      create or replace procedure ifcopied(val varchar)
                                         returns varchar
                                         language javascript
                                         execute as caller
                                         as
                                         $$
                                         sql_command = "select * from t1 where col = '" + VAL + "'";
                                         var stmt = snowflake.createStatement({sqlText:sql_command});                       
                                         var res = stmt.execute();
                                         res.next();
                                         row_status = res.getColumnValue(1);
                                         return row_status                                   
                                         $$;
      

      推荐使用绑定变量的版本:

      create or replace procedure ifcopied(val varchar)
                                         returns varchar
                                         language javascript
                                         execute as caller
                                         as
                                         $$
                                         sql_command = "select * from t1 where col = ?";
                                         var stmt = snowflake.createStatement({sqlText:sql_command, binds:[ VAL ]});                       
                                         var res = stmt.execute();
                                         res.next();
                                         row_status = res.getColumnValue(1);
                                         return row_status                                   
                                         $$;
      

      【讨论】:

        猜你喜欢
        • 2020-08-06
        • 2021-07-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-28
        • 2014-04-30
        • 1970-01-01
        相关资源
        最近更新 更多