雪花声明在程序中不起作用
Posted
技术标签:
【中文标题】雪花声明在程序中不起作用【英文标题】:Snowflake Statement not working within procedure 【发布时间】:2021-09-09 12:34:17 【问题描述】:我写了一个 SQL 语句。当我正常执行该语句时,它可以工作。但在存储过程中,我收到以下错误:
SQL 编译错误:位置 43 处的错误第 1 行无效标识符 'PA.S_ID' 在 Statement.execute 第 13 行位置 25
CREATE OR REPLACE PROCEDURE DWH.TEST.FILL_BUSHARES()
RETURNS VARCHAR
LANGUAGE javascript
EXECUTE AS OWNER
AS
$$
function executeSQL(SQL )
var rows_affected = 0;
// Prepare statement.
var stmt = snowflake.createStatement(
sqlText: SQL
);
// Execute Statement
var res = stmt.execute();
rows_affected = res.getNumRowsAffected();
return rows_affected;
sql_fall1 = executeSQL("INSERT INTO DWH.TEMP.CM_BU_SHARES "+
" SELECT pa.S_ID, " +
" pa.PRCTR, " +
" pa.MATNR, " +
" pa.KUNNR " +
" ,100 as invalid_Share --00 " +
" ,0 as OFIS_Share --10 " +
" ,0 as CREHO_Share --20 " +
" ,0 as COWO_Share --30 " +
" ,0 as TECH_Share --40 " +
" ,0 as Lifestyle_Share --50 " +
" ,0 as NISO_Share --60 " +
"FROM DWH.TEMP.CMPREP_PROFITABILITYANALYSIS as PA "
);
var returnStatement = "test" ;
return returnStatement;
$$;
但是声明是正确的。如果我复制语句将其添加到 Notepad++、crtl+h 并将所有 " 替换为空,所有 + 替换为空,然后在经典控制台中运行它。
"INSERT INTO DWH.TEMP.CM_BU_SHARES "+
" SELECT pa.S_ID, " +
" pa.PRCTR, " +
" pa.MATNR, " +
" pa.KUNNR " +
" ,100 as invalid_Share --00 " +
" ,0 as OFIS_Share --10 " +
" ,0 as CREHO_Share --20 " +
" ,0 as COWO_Share --30 " +
" ,0 as TECH_Share --40 " +
" ,0 as Lifestyle_Share --50 " +
" ,0 as NISO_Share --60 " +
"FROM DWH.TEMP.CMPREP_PROFITABILITYANALYSIS PA "
我无法解释为什么它不能在存储过程中工作。谷歌没有帮助。标识符没有错。它不区分大小写。相同的查询在没有 " 和 + 的情况下也可以工作。
【问题讨论】:
您的第一条和第二条语句不同。您在第二个中选择的第一列的第一个为 NULL 是不同的 在 JavaScript 中,尤其是在编写 Snowflake 存储过程时,使用反引号 ` 来开始和结束字符串会使事情变得更具可读性。您可以编写多行字符串而不用“+”连接。您可以在字符串中同时使用单引号和双引号,而无需转义它们。您还可以使用 $myVariable 命名法替换上下文中任何变量的值。 @Brad True。在此示例中,语句不同。我将 NULL 添加为第一列,以查看它是一般问题还是 S_ID 有问题。但是,当我将 S_ID 替换为 NULL 时,错误出现在第二行并显示“无效标识符 'PA.PRCTR'”我更正了第一条语句 @GregPavlik 解决了我的问题。现在可以了。非常感谢!!!但是你能详细说明如何在带有反引号的字符串中获取变量吗?例如我有一个 var rows_written = 7 现在我想构建一个这样的字符串: var returnText = "Number of rows written" + rows_written "to tableX" 但是使用反引号的语法是: var returnText = `Number of rowswritten $rows_written 到 tableX` 对吗? @Schnurres,是的,这是正确的语法(只需删除右大括号前的空格)。 【参考方案1】:我认为您的评论标志正在破坏 SQL。你可以试试这个吗?
sql_fall1 = executeSQL("INSERT INTO DWH.TEMP.CM_BU_SHARES "+
" SELECT pa.S_ID, " +
" pa.PRCTR, " +
" pa.MATNR, " +
" pa.KUNNR " +
" ,100 as invalid_Share --00 \n" +
" ,0 as OFIS_Share --10 \n" +
" ,0 as CREHO_Share --20 \n" +
" ,0 as COWO_Share --30 \n" +
" ,0 as TECH_Share --40 \n" +
" ,0 as Lifestyle_Share --50 \n" +
" ,0 as NISO_Share --60 \n" +
"FROM DWH.TEMP.CMPREP_PROFITABILITYANALYSIS as PA "
);
如果还是失败,可以不用cmets运行吗?
sql_fall1 = executeSQL("INSERT INTO DWH.TEMP.CM_BU_SHARES "+
" SELECT pa.S_ID, " +
" pa.PRCTR, " +
" pa.MATNR, " +
" pa.KUNNR " +
" ,100 as invalid_Share " +
" ,0 as OFIS_Share " +
" ,0 as CREHO_Share " +
" ,0 as COWO_Share " +
" ,0 as TECH_Share " +
" ,0 as Lifestyle_Share " +
" ,0 as NISO_Share " +
"FROM DWH.TEMP.CMPREP_PROFITABILITYANALYSIS as PA "
);
【讨论】:
以上是关于雪花声明在程序中不起作用的主要内容,如果未能解决你的问题,请参考以下文章
Snowflake METADATA$FILENAME 关键字在 snowsql(Unix 命令提示符)中不起作用,但在雪花 UI 中起作用