我的过程是将数据拖放到天蓝色blob上的csv文件中。 SQL代码可以正常运行,但是在实施到过程中时出现以下错误:
JavaScript compilation error: Uncaught SyntaxError: Invalid or unexpected token in TST_30102020 at ' sqlText:'COPY INTO @storage/folder/file_1.csv' position 19
我找不到破坏整个事物的奇数符号。
过程如下:
CREATE or REPLACE PROCEDURE TST_30102020()
RETURNS VARCHAR
LANGUAGE JAVASCRIPT
EXECUTE AS CALLER
AS
$$
var stmt = snowflake.createStatement({
sqlText:'COPY INTO @storage/folder/file_1.csv
FROM (SELECT t1.ATTR,t1.ATTR, t2.ATTR, t2.ATTR, t1.ATTR, t1.MIASTO, t1.ATTR, t1.ATTR,
t1.ATTR, t1.ATTR, t1.ATTR, t1.ATTR, t1.ATTR, t1.ATTR,
t1.ATTR, t1.ATTR, t1.ATTR, t1.ATTR, t1.ATTR, t1.KONCESJA_A, t1.ATTR, t1.ATTR
FROM t1
LEFT JOIN (SELECT * FROM table WHERE ATTR >= '2020-09-01') AS t2
ON t1.ATTR= t2.ATTR AND t1.ATTR= t2.ATTR
LEFT JOIN (SELECT * FROM table WHERE ATTR>= '2020-09-01') AS t3
ON t2.ATTR= t3.ATTR
AND t2.ATTR= t3.ATTR
AND t2.ATTR= t3.ATTR
WHERE t2.ATTR> 0
ORDER BY t2.ATTR, t1.ATTR)
FILE_FORMAT = (TYPE = 'CSV')
OVERWRITE = TRUE
SINGLE = TRUE
MAX_FILE_SIZE = 170000000'});
try {
stmt.execute();
return "Succeeded.";
}
catch (err) {
return "Failed: " + err;
}
$$;
答案 0 :(得分:2)
问题是您正在用单引号打开JavaScript字符串,并且您的字符串包含单引号。这也是多行文字。好消息是JavaScript提供了一种解决此问题的好方法。将字符串的开头和结尾的引号从单引号更改为反引号。反勾号位于美国键盘的左上方,在tilda〜字符下方。
使用倒勾将允许多行字符串,包括SQL语句。它还可以通过在任何JavaScript变量之前添加一个美元符号并将其用花括号括起来来进行令牌替换。换句话说,如果您有一个名为myVariable的变量,则可以通过在字符串中的任意位置放置$ {myVariable}来替换该值,前提是您使用反斜杠打开和关闭了字符串文字。
CREATE or REPLACE PROCEDURE TST_30102020()
RETURNS VARCHAR
LANGUAGE JAVASCRIPT
EXECUTE AS CALLER
AS
$$
var stmt = snowflake.createStatement({
sqlText:`COPY INTO @storage/folder/file_1.csv
FROM (SELECT t1.ATTR,t1.ATTR, t2.ATTR, t2.ATTR, t1.ATTR, t1.MIASTO, t1.ATTR, t1.ATTR,
t1.ATTR, t1.ATTR, t1.ATTR, t1.ATTR, t1.ATTR, t1.ATTR,
t1.ATTR, t1.ATTR, t1.ATTR, t1.ATTR, t1.ATTR, t1.KONCESJA_A, t1.ATTR, t1.ATTR
FROM t1
LEFT JOIN (SELECT * FROM table WHERE ATTR >= '2020-09-01') AS t2
ON t1.ATTR= t2.ATTR AND t1.ATTR= t2.ATTR
LEFT JOIN (SELECT * FROM table WHERE ATTR>= '2020-09-01') AS t3
ON t2.ATTR= t3.ATTR
AND t2.ATTR= t3.ATTR
AND t2.ATTR= t3.ATTR
WHERE t2.ATTR> 0
ORDER BY t2.ATTR, t1.ATTR)
FILE_FORMAT = (TYPE = 'CSV')
OVERWRITE = TRUE
SINGLE = TRUE
MAX_FILE_SIZE = 170000000`});
try {
stmt.execute();
return "Succeeded.";
}
catch (err) {
return "Failed: " + err;
}
$$;
最后的提示...在Stackoverflow上检查代码样本的颜色编码。您会注意到,您提供的那个遇到日期时会更改颜色,因为那里有一个单引号字符。即使在修改后的代码中用反勾号打开和关闭报价时,它仍保持相同的颜色(仅对其进行了更改)。