简体   繁体   English

通过 UDF 表插入到表中,不能从存储过程中工作

[英]Insert into table via UDF table not working from stored procedure

My stored procedure should insert data into table via UDF.我的存储过程应该通过 UDF 将数据插入表中。

The string I generate is correct when I run it as SQL statement, but when I execute it through the stored procedure, it throws a syntax error.我生成的字符串作为 SQL 语句运行时是正确的,但是当我通过存储过程执行它时,它会抛出一个语法错误。

This is the SQL statement:这是 SQL 语句:

insert into dbname.public.TBLPROVIDER 
    select * 
    from table(f_TBLPROVIDER('dbname',cast('20211031233226' as bigint)));

Here is the code which is failing from the stored procedure这是存储过程失败的代码

var sql_tbl_populate  = "insert into " + S_DB_NAME + ".public." + TBL_NAME + " "
sql_tbl_populate += "select * from table(f_" + TBL_NAME +"('"+ S_DB_NAME + "',cast('" + S_TIMESTAMP + "' as bigint)));"
stmt_sql_tbl_populate = snowflake.createStatement({sqlText: sql_tbl_populate} );
stmt_sql_tbl_populate.execute();

The error is错误是

Execution error in stored procedure SP_POPULATE_TRGT_TABLES: SQL compilation error: syntax error line 1 at position 105 unexpected 'dbname'.存储过程 SP_POPULATE_TRGT_TABLES 中的执行错误:SQL 编译错误:语法错误第 1 行在位置 105 意外的“dbname”。
At Statement.execute, line 21 position 23在 Statement.execute,第 21 行位置 23

I am able to locate your query and find out the DDL of your SP.我能够找到您的查询并找出您的 SP 的 DDL。

The issue lies on the below query when you tried to insert into the tbl_log table:当您尝试插入 tbl_log 表时,问题在于以下查询:

var sqld = "insert into log_table values ('" + sql_tbl_populate + "')";

The actually failed query was:实际失败的查询是:

insert into log_table values ('insert into dbname.public.tablename select * from table(f_TBLPROVIDER('dbname',cast('20211031233226' as bigint)));')

Because you had single quotes around the variable "sql_tbl_populate", which already had the single quote for the parameters in the function f_TBLPROVIDER.因为您在变量“sql_tbl_populate”周围有单引号,该变量已经对函数 f_TBLPROVIDER 中的参数使用了单引号。

You need to escape the single quotes inside "sql_tbl_populate" when you build your INSERT statement in variable "sqld".在变量“sqld”中构建 INSERT 语句时,需要对“sql_tbl_populate”中的单引号进行转义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM