如何在Snowflake存储过程中将数组插入为绑定元素?

huangapple go评论44阅读模式
英文:

how to insert the array as bind element in snowflake stored procedure?

问题

以下代码抛出错误,原因是undefined bind element,因为数组包含在引号中的字符串,例如:["str1","str2","str3"]

CREATE OR REPLACE PROCEDURE sp_array(ARG ARRAY)
RETURNS VARCHAR
LANGUAGE JAVASCRIPT
AS
$$

let stmt = snowflake.createStatement({sqlText: `INSERT INTO TABLE1( VARIANT_COLUMN) SELECT ?`},binds:ARG})
stmt.execute();
$$;
英文:

The below code is throwing an error as undefined bind element `because array contains string which is enclosed in quotes eg: ["str1","str2","str3"]

CREATE OR REPLACE PROCEDURE sp_array(ARG ARRAY)
RETURNS VARCHAR
LANGUAGE JAVASCRIPT
AS
$$

let stmt = snowflake.createStatement({sqlText: `INSERT INTO TABLE1( VARIANT_COLUMN) SELECT ?`},binds:ARG})
stmt.execute();
$$;

答案1

得分: 1

您可以传递一个数组,但数组不受支持作为绑定变量。您可以使用JSON.stringify将其转换为字符串,字符串受支持作为绑定变量。然后,您可以使用parse_json将其在SQL语句中转换为数组。这将强制存储过程的调用者提供有效的variant,因为这是指定的类型。但是,如果您想强制调用者提供有效的数组,您可以将输入类型更改为 "array",这将更紧密地定义类型。

create or replace table table1 (variant_column variant);

CREATE OR REPLACE PROCEDURE sp_array(ARG ARRAY)
RETURNS VARCHAR
LANGUAGE JAVASCRIPT
AS
$$
let stringifiedArray = JSON.stringify(ARG);
let stmt = snowflake.createStatement({sqlText: `INSERT INTO TABLE1( VARIANT_COLUMN) SELECT parse_json(?)`,binds:[ARG]});
stmt.execute();
$$;

call sp_array(array_construct('str1', 'str2', 'str3')); 
call sp_array(['str1','str2','str3']);

select * from table1;
英文:

You can pass in an array, but arrays are not supported as bind variables. You can use JSON.stringify to convert it to a string, which is supported as a bind variable. You can then use parse_json to convert it to an array in the SQL statement. This will force the caller of the SP to supply a valid variant since that's the type specified. However, if you want to force the caller to supply a valid array, you can change the input type to "array" and this is more tightly typed.

create or replace table table1 (variant_column variant);

CREATE OR REPLACE PROCEDURE sp_array(ARG ARRAY)
RETURNS VARCHAR
LANGUAGE JAVASCRIPT
AS
$$
let stringifiedArray = JSON.stringify(ARG);
let stmt = snowflake.createStatement({sqlText: `INSERT INTO TABLE1( VARIANT_COLUMN) SELECT parse_json(?)`,binds:[ARG]});
stmt.execute();
$$;

call sp_array(array_construct('str1', 'str2', 'str3')); 
call sp_array(['str1','str2','str3']);

select * from table1;

答案2

得分: 0

使用 TO_JSONPARSE_JSON:

创建或替换表 TABLE1(VARIANT_COLUMN VARIANT);

创建或替换过程 sp_array(ARG TEXT)
返回 VARCHAR
LANGUAGE JAVASCRIPT

$$

let stmt = snowflake.createStatement({
sqlText:INSERT INTO TABLE1(VARIANT_COLUMN)SELECT PARSE_JSON(?)
,binds:[ARG]}})
stmt.execute();
$$;

CALL sp_array(TO_JSON([1,2,3]));

CALL sp_array(TO_JSON([&39;a&39;,&39;b&39;]));

SELECT * FROM TABLE1;
-- [1, 2, 3]
-- ["a", "b"]

英文:

Using TO_JSON and PARSE_JSON:

CREATE OR REPLACE TABLE TABLE1( VARIANT_COLUMN VARIANT);

CREATE OR REPLACE PROCEDURE sp_array(ARG TEXT)
RETURNS VARCHAR
LANGUAGE JAVASCRIPT
AS
$$

let stmt = snowflake.createStatement({
    sqlText:`INSERT INTO TABLE1( VARIANT_COLUMN) SELECT PARSE_JSON(?)`
   ,binds:[ARG]})
stmt.execute();
$$;

CALL sp_array(TO_JSON([1,2,3]));

CALL sp_array(TO_JSON(['a', 'b']));


SELECT * FROM TABLE1;
-- [   1,   2,   3 ]
-- [   "a",   "b" ]

huangapple
  • 本文由 发表于 2023年4月6日 22:23:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75950639.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定