gpt4 book ai didi

javascript - 合并语句和存储过程

转载 作者:行者123 更新时间:2023-11-30 19:03:12 25 4
gpt4 key购买 nike

我正在尝试使用 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 compilation error: Uncaught SyntaxError: Invalid or unexpected token in INSERT_GROUPS at 'var sql_cmd = 'Merge into database.events.groups et using' position 14

最佳答案

问题是 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.

}
$$;

关于javascript - 合并语句和存储过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59276002/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com