I am trying to write a stored procedure with a Merge
statement.
It looks something like this:
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.
}
$$;
The stored procedure was created, but if I run it, I am getting some syntax error. The escaping characters were escaped and the query was getting executed when I run it manually (with using the stored procedure).
Here is the error I am getting
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