2

I am having difficultly finding documentation on how to insert data into an ARRAY column type using SQL on a Snowflake table.

Snowflake Documentation: https://docs.snowflake.net/manuals/sql-reference/data-types-semistructured.html#array

// example table
CREATE OR REPLACE TABLE array_test_table (
 id number,
 ids array
);
// This does not work
INSERT INTO array_test_table (id, ids) 
VALUES (1, '[1,2,3]' );

I have tried using using postgres's syntax like ones described here: https://stackoverflow.com/questions/33335338/inserting-array-values

I originally asked this question here: Snowflake Community

asked Sep 13, 2018 at 15:19

2 Answers 2

4

It looks like you need to apply a function to parse the array. As of 9/13/2018 functions are not allowed in value lists see Is there a literal syntax for arrays

One way around this is to combine INSERT INTO and SELECT statement. Example:

CREATE OR REPLACE TABLE array_test_table (
 id number,
 ids array
);
INSERT INTO array_test_table (id, ids) 
SELECT 1,ドル PARSE_JSON(2ドル)
 FROM VALUES (1, '[1,2,3]' );
answered Sep 13, 2018 at 15:19
2

Another option is to use ARRAY_CONSTRUCT(1,2,3) instead of parsing JSON.

INSERT INTO array_test_table (id, ids) 
SELECT 1, ARRAY_CONSTRUCT(1,2,3) ; 

https://docs.snowflake.com/en/sql-reference/functions/array_construct.html

mustaccio
28.6k24 gold badges60 silver badges77 bronze badges
answered Jan 24, 2022 at 23:11

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.