简体   繁体   English

雪花存储过程错误 - 未设置绑定变量

[英]Snowflake stored procedure error - bind variable not set

I have a stored procedure that tracks new records inserted in a table (change tracking is on).我有一个存储过程来跟踪插入表中的新记录(更改跟踪已打开)。 Pretty simple, code shown here:很简单,这里显示的代码:

create or replace procedure sp_audit_insert_counts(TABLENAME varchar, FILTER varchar, 

STARTTIME timestamp_ltz, ENDTIME timestamp_ltz)
returns integer
language sql
as
$$
declare 
iInsertCount integer default 0;
begin

select count(*) into :iInsertCount from identifier(:TABLENAME) changes(information => default) at(timestamp => :STARTTIME) end(timestamp => :ENDTIME) where metadata$action = 'INSERT' and metadata$isupdate = 'FALSE';

return iInsertCount;
end;
$$
;

However when I run the procedure, I get an error但是,当我运行该过程时,出现错误

Bind variable:STARTTIME not set绑定变量:未设置STARTTIME

call sp_audit_insert_counts('TEST_TABLE', null, to_timestamp('2022-08-23 00:00:00'), to_timestamp('2022-08-23 09:00:00'))

If I hard code the time stamps in the procedure it work fine.如果我在过程中对时间戳进行硬编码,它可以正常工作。 What could be wrong?有什么问题?

Thanks in advance提前致谢

Regards Sid问候希德

This is the best solution I could come up with:这是我能想到的最佳解决方案:

set x_end = '2030-08-24 22:01:00'::timestamp;
set x_start = '1900-08-24 22:00:00'::timestamp;

create or replace procedure sp_audit_insert_counts(TABLENAME varchar, FILTER varchar, 
STARTTIME timestamp_tz, ENDTIME timestamp_tz)
returns integer
language sql
execute as caller
as
$$
declare 
iInsertCount integer default 0;
begin

set x_start = :STARTTIME;
set x_end = :ENDTIME;

select count(*) into :iInsertCount from identifier(:TABLENAME) changes(information => default) at(timestamp => $x_start) end(timestamp => $x_end) where metadata$action = 'INSERT' and metadata$isupdate = 'FALSE';


return iInsertCount;
end;
$$
;

To make it work I had to:为了使它工作,我必须:

  • Create the variables before creating the stored procedure.在创建存储过程之前创建变量。
  • execute as caller to work with session variables. execute as caller以使用 session 变量。
  • Use timestamp_tz to avoid conversion weirdness.使用timestamp_tz来避免转换异常。

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

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