Snowflake Create Procedure returns "Syntax error: unexpected 'variableName'" when trying to use variable in an IF statement

huangapple go评论68阅读模式
英文:

Snowflake Create Procedure returns "Syntax error: unexpected 'variableName'" when trying to use variable in an IF statement

问题

我有以下存储过程,我正试图在 Snowflake 新控制台中创建。

CREATE OR REPLACE PROCEDURE _PTEST(ip VARCHAR)
RETURNS STRING
LANGUAGE SQL
AS
BEGIN
    -- 初始化临时表
    CREATE OR REPLACE TEMPORARY TABLE _temp_products (Product VARCHAR);
    
    -- 检查传递的值并相应地填充表格
    IF ip = 'All' THEN
        INSERT INTO _temp_products (Product) VALUES ('value1'), ('value2'), ('value3');
        RETURN 'All';
    ELSE
        INSERT INTO _temp_products (Product) VALUES (ip);
        RETURN ip;
    END IF;
END;

当我尝试执行代码时,我收到以下错误消息:

错误截图

我具有作为角色的所有授权,并且我正在正确的数据库和模式中编写。为什么会出现此错误?

我还尝试将语句放在括号 () 之间,并将 SQL 脚本作为字符串传递,并在开头和结尾放置 $$。

问题似乎与在 IF 语句内部使用变量 ip 有关。

例如,以下代码按预期工作:

CREATE OR REPLACE PROCEDURE _PEST2(ip VARCHAR)
RETURNS STRING
LANGUAGE SQL
AS
BEGIN
    RETURN ip;
END;

并且可以使用以下方式调用:

CALL _PTEST2('pippo');
英文:

I have the following stored procedure which I am trying to create in Snowflake new console.

CREATE OR REPLACE PROCEDURE _PTEST(ip VARCHAR)
RETURNS STRING
LANGUAGE SQL
AS
BEGIN
    -- initialize the temporary table
    CREATE OR REPLACE TEMPORARY TABLE _temp_products (Product VARCHAR);
    
    -- check which value is passed and populate the table accordingly
    IF ip = 'All' THEN
        INSERT INTO _temp_products (Product) VALUES ('value1'), ('value2'), ('value3');
        RETURN 'All';
    ELSE
        INSERT INTO _temp_products (Product) VALUES (ip);
        RETURN ip;
    END IF;
END;

When I try to execute the code I get the follwoing error

Screensot of the error

I have all the authorization as a role and I am writing in the correct database and schema.
Why do I get this error?

I also tried to put the statement in between brackets () and to pass the SQL script as string putting $$ at the beginning and at the end.

The problem seems to be linked to the fact that the variable ip is used inside the IF statement.

For example the following code works as expected:

CREATE OR REPLACE PROCEDURE _PEST2(ip VARCHAR)
RETURNS STRING
LANGUAGE SQL
AS
BEGIN
    RETURN ip;
END;

and this can be called with

CALL _PTEST2('pippo');

答案1

得分: 0

在SQL语句中,参数被指定为:ip

IF ip = 'All' THEN 应该改为 IF (ip = 'All') THEN

CREATE OR REPLACE PROCEDURE _PTEST(ip VARCHAR)
RETURNS STRING
LANGUAGE SQL
AS
BEGIN
-- 初始化临时表
CREATE OR REPLACE TEMPORARY TABLE _temp_products (Product VARCHAR);

IF (ip = 'All') THEN
INSERT INTO _temp_products (Product) VALUES ('value1'), ('value2'), ('value3');
RETURN 'All';
ELSE
INSERT INTO _temp_products (Product) VALUES (:ip);
RETURN ip;
END IF;
END;

英文:

Two things need to be fixed :

In SQL statement the argument is specified as :ip

IF ip = 'All' THEN should be IF (ip = 'All') THEN

CREATE OR REPLACE PROCEDURE _PTEST(ip VARCHAR)
RETURNS STRING
LANGUAGE SQL
AS
BEGIN
    -- initialize the temporary table
    CREATE OR REPLACE TEMPORARY TABLE _temp_products (Product VARCHAR);
    
    IF (ip = 'All') THEN
        INSERT INTO _temp_products (Product) VALUES ('value1'), ('value2'), ('value3');
        RETURN 'All';
    ELSE
        INSERT INTO _temp_products (Product) VALUES (:ip);
        RETURN ip;
    END IF;
END;

huangapple
  • 本文由 发表于 2023年5月17日 18:06:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76270912.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定