我正在尝试使用Merge
语句编写存储过程。
看起来像这样:
create or replace procedure database.events.insert_groupss()
returns string
LANGUAGE JAVASCRIPT
as
$$
var sql_cmd = 'Merge into database.events.groups et using
(select lower(f.VALUE:id::string) as id,
min(f.VALUE:time_timestamp::timestamp) as event_timestamp,
f.VALUE:service_string::string as service,
f.VALUE:title_string::string as title,
min(X.VALUE:created_at::timestamp) as title_ts ,
X.VALUE:group::string as group
from \'@database.sources.s3stage/version=3/stream=live/year=2019/month=12/\'
(file_format => \'oak.public.JSON_FORMAT\' ), lateral flatten (input => $1:group_events, RECURSIVE => TRUE) f
, LATERAL FLATTEN(parse_json(replace(replace(replace(replace(f.value:groups ,\'"[\', \'[\'),\'=>\',\':\'),\'\\\\\',\'\'),\']"\',\']\'))) x
where id is not null and group is not null
group by id,group,service,title)a
on et.id=a.id and et.group = a.group
when not matched then Insert (id , event_timestamp , service , title , title_ts ,group )
values (a.id , a.event_timestamp , a.service , a.title , a.title_ts ,a.group );';
try {
snowflake.execute ({sqlText: sql_cmd});
return "Succeeded";
}
catch (err) {
return "Failed: " + err; // Return a success/error indicator.
}
$$;
已创建存储过程,但是如果我运行它,则会收到一些语法错误。当我手动(使用存储过程)运行转义字符时,转义字符被转义并且查询正在执行。
这是我遇到的错误
JavaScript编译错误:语法错误:INSERT_GROUPS中的无效或意外令牌,位于'var sql_cmd ='合并到database.events.groups中,并使用'位置14
答案 0 :(得分:1)
问题是sql_cmd在多行中,因此您需要连接,或者应该在单行中。
create or replace procedure database.events.insert_groupss()
returns string
LANGUAGE JAVASCRIPT
as
$$
var sql_cmd = 'Merge into database.events.groups et using'
sql_cmd += ' (select lower(f.VALUE:id::string) as id,'
sql_cmd += ' min(f.VALUE:time_timestamp::timestamp) as event_timestamp,'
sql_cmd += ' f.VALUE:service_string::string as service,'
sql_cmd += ' f.VALUE:title_string::string as title,'
sql_cmd += ' min(X.VALUE:created_at::timestamp) as title_ts ,'
sql_cmd += ' X.VALUE:group::string as group'
sql_cmd += ' from \'@database.sources.s3stage/version=3/stream=live/year=2019/month=12/\''
sql_cmd += ' (file_format => \'oak.public.JSON_FORMAT\' ), lateral flatten (input => $1:group_events, RECURSIVE => TRUE) f'
sql_cmd += ' , LATERAL FLATTEN(parse_json(replace(replace(replace(replace(f.value:groups ,\'"[\', \'[\'),\'=>\',\':\'),\'\\\\\',\'\'),\']"\',\']\'))) x'
sql_cmd += ' where id is not null and group is not null'
sql_cmd += ' group by id,group,service,title)a'
sql_cmd += ' on et.id=a.id and et.group = a.group'
sql_cmd += ' when not matched then Insert (id , event_timestamp , service , title , title_ts ,group )'
sql_cmd += ' values (a.id , a.event_timestamp , a.service , a.title , a.title_ts ,a.group );";'
try {
snowflake.execute ({sqlText: sql_cmd});
return "Succeeded";
}
catch (err) {
return "Failed: " + err; // Return a success/error indicator.
}
$$;
答案 1 :(得分:0)
问题在try子句中,SQL变量名称不正确。在下面替换
snowflake.execute ({sqlText: sql_cmd})
非常感谢, 斯里加