简体   繁体   中英

Snowflake: How to insert into a table from a flattened array of date strings variable?

Brand new to Snowflake. I need to insert all the dates from an array into a date field in a table. I'm doing this through a JavaScript Stored Proc.

//get the array of dates
datesMissingArr = priorDatesArr.filter(date=>summDateArr.indexOf(date)==-1);

datesMissingArr now equals this: [ "2019-10-08", "2019-09-08", "2019-10-06" ]

Now I want to take that and insert each one of those dates into a table, like so:

snowflake.createStatement( { sqlText: `INSERT INTO "MYDB"."PUBLIC"."dates_table"("logdate") 
                             SELECT value::date FROM TABLE(FLATTEN(input => parse_json('${datesMissingArr}')));`                                         
                            } ).execute();

But I get an error:

Error parsing JSON: garbage in the numeric literal: 2019-10-08,, pos 11 At Statement.execute

Some advice/help appreciated.

Thanks!

You need to convert the Array to a JSON string before calling execute() :
var datesMissingStr = JSON.stringify(datesMissingArr); and then use ${datesMissingStr} .

But really you should bind the parameter:

snowflake.createStatement( { 
    sqlText: `INSERT INTO "dates_table"("logdate") 
              SELECT value::date FROM TABLE(FLATTEN(input => parse_json(:1)))`,
    binds:   [JSON.stringify(datesMissingArr)]
} ).execute();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM