开发者问题收集

Snowflake JS 过程

2021-09-21
114
create or replace procedure test_09172(c_custkey varchar(25)
     ,c_mktsegment varchar(25)
     ,cname varchar(25)) returns string not null   language javascript   execute as owner   as   $$
      var sqlquery="";
       var fltConvUomPK="";
       var fltConvFactorPK="";
       var ParentClass="";
       var VMAJOR="";
       var VMINOR="";
              try   {    
         var sql_command  =`SELECT C_ADDRESS,C_NATIONKEY
                        from customers                  
                        WHERE c_custkey=C_CUSTKEY and c_name=CNAME and C_MKTSEGMENT=C_MKTSEGMENT`;
                                 var rs=snowflake.createStatement( {sqlText: sql_command});
     
       var result_set1 = rs.execute();
            }
     catch(err)
     {
     return err.message
     }
     return rs.getSqlText();   $$;

执行“call test_09172('537289','FURNITURE','Customer#000537289');”时

我收到以下错误。

JavaScript execution error: Uncaught TypeError: Cannot read property
 'getSqlText' of undefined in TEST_09172 at ' return rs.getSqlText();'
 position 14 stackstrace: TEST_09172 line: 28

请帮我修复此问题

2个回答

错误似乎与未定义的对象有关,但当我尝试重现它时,您的代码没有任何错误。

我注意到您没有将参数绑定到 SQL:

var sql_command  =`SELECT C_ADDRESS,C_NATIONKEY
from customers                  
WHERE c_custkey=C_CUSTKEY and c_name=CNAME and C_MKTSEGMENT=C_MKTSEGMENT`;

SQL 不区分大小写,因此您只需将列与它们自己进行比较( c_custkey=C_CUSTKEY 和 C_MKTSEGMENT=C_MKTSEGMENT )。c_name=CNAME 可能会产生错误。

为了避免列名和参数名混淆,我重写了查询:

create or replace procedure test_09172(c_custkey_p varchar(25)
,c_mktsegment_p varchar(25)
,c_name_p varchar(25)) returns string not null   language javascript   execute as owner   as   $$
var sqlquery="";
var fltConvUomPK="";
var fltConvFactorPK="";
var ParentClass="";
var VMAJOR="";
var VMINOR="";
      try   {    
         var sql_command  =`SELECT C_ADDRESS,C_NATIONKEY
                      from customers                    
                      WHERE c_custkey=? and c_name=? and C_MKTSEGMENT=?`;

         var rs=snowflake.createStatement( {sqlText: sql_command , binds:[ C_CUSTKEY_P, C_NAME_P , C_MKTSEGMENT_P ] });
    }
catch(err)
{
return err.message
}
return rs.getSqlText();   
$$;

在我的测试中,它按预期工作,但我没有您的数据,所以您应该测试它。

Gokhan Atil
2021-09-21

方法 getSqlText 适用于 Statement 对象,而不是 ResultSet ,请参阅 此处

getSqlText()

此方法返回 Statement 对象中已准备好的查询的文本。

Sergiu
2021-09-21