Snowflake SQL存储过程创建报错求助:MAX_DATE标识符无效
Snowflake SQL存储过程变量报错解决方案
报错原因
你使用SET定义的是会话级变量,在存储过程中引用时未添加$前缀,Snowflake会将其识别为无效标识符,因此触发报错。
解决方案1:修正会话变量引用方式
在引用SET定义的变量时,需要在变量名前添加$符号:
create or replace procedure get_max_date( ) returns datetime not null language sql as $$ begin set max_date = (select max(last_updated) from Control_Variables); return $max_date; -- 添加$前缀引用会话变量 end $$;
解决方案2:使用本地变量(推荐)
更规范的方式是通过DECLARE声明存储过程内部的本地变量,仅在过程内生效,避免会话变量污染:
create or replace procedure get_max_date( ) returns datetime not null language sql as $$ declare max_date datetime; -- 声明本地变量 begin select max(last_updated) into max_date from Control_Variables; -- 通过into赋值 return max_date; end $$;
内容的提问来源于stack exchange,提问作者Nadeem
相关产品推荐
相关产品推荐

