Snowflake JavaScript SP 不支持官方文档中描述的所有方法
2022-01-27
359
我尝试在 Snowflake 中创建一个执行
INSERT INTO
语句的 JavaScript 存储过程。因此,我希望收到已插入目标表的行数。Snowflake 文档描述了我应该能够通过将方法
getNumRowsInserted()
应用于对象
Statement
来实现这一点。但它似乎根本不起作用。
这是一个例子:
CREATE OR REPLACE TEMP TABLE my_little_test (x INT, y INT);
INSERT INTO my_little_test
SELECT 1,2;
CREATE OR REPLACE PROCEDURE merge_test_sp()
RETURNS VARIANT
LANGUAGE JAVASCRIPT
EXECUTE AS CALLER
AS
$$
var result = {"num_rows":""};
var stmt = "INSERT INTO my_little_test SELECT 3, 4";
var exec_stmt = snowflake.createStatement({sqlText:stmt}).execute();
result["num_rows"] = exec_stmt.getNumRowsInserted();
return result;
$$
;
CALL merge_test_sp();
它抛出了这个错误消息:
JavaScript execution error: Uncaught TypeError: exec_stmt.getNumRowsInserted is not a function in MERGE_TEST_SP at 'result["num_rows"] = exec_stmt.getNumRowsInserted();' position 31 stackstrace: MERGE_TEST_SP line: 9
但是应用方法
getNumRowsAffected
有效:
CREATE OR REPLACE TEMP TABLE my_little_test (x INT, y INT);
INSERT INTO my_little_test
SELECT 1,2;
CREATE OR REPLACE PROCEDURE merge_test_sp()
RETURNS VARIANT
LANGUAGE JAVASCRIPT
EXECUTE AS CALLER
AS
$$
var result = {"num_rows":""};
var stmt = "INSERT INTO my_little_test SELECT 3, 4";
var exec_stmt = snowflake.createStatement({sqlText:stmt}).execute();
// this method somehow works ?!
result["num_rows"] = exec_stmt.getNumRowsAffected();
return result;
$$
;
CALL merge_test_sp();
输出为
{"num_rows": 1
。
此外,其他方法也不起作用,因为我尝试使用
MERGE INTO
语句进行同样的事情。
getNumDuplicateRowsUpdated
、
getNumRowsDeleted
、
getNumRowsUpdated
都不起作用。只有信息量较少的方法
getNumRowsAffected()
有效。
有人遇到过同样的问题吗?有人知道解决方法吗?
顺便说一句:这是我所参考的文档的链接: https://docs.snowflake.com/en/sql-reference/stored-procedures-api.html#object-statement
谢谢,并致以最诚挚的问候
1个回答
你可以试试这个吗?
CREATE OR REPLACE TEMP TABLE my_little_test (x INT, y INT);
INSERT INTO my_little_test
SELECT 1,2;
CREATE OR REPLACE PROCEDURE merge_test_sp()
RETURNS VARIANT
LANGUAGE JAVASCRIPT
EXECUTE AS CALLER
AS
$$
var result = {"num_rows":""};
var stmt = "INSERT INTO my_little_test SELECT 3, 4";
var exec_stmt = snowflake.createStatement({sqlText:stmt});
exec_stmt.execute();
result["num_rows"] = exec_stmt.getNumRowsInserted();
return result;
$$
;
CALL merge_test_sp();
你通过运行 snowflake.createStatement({sqlText:stmt}).execute() 将结果集分配给 exec_stmt,这就是为什么它没有 getNumRowsInserted 方法不可用的原因。
Gokhan Atil
2022-01-27