英文:
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
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;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论