Can we pass list or array as parameter to JavaScript procedure in snowflake?
Asked Answered
R

2

5

Can we pass list or array as parameter to javascript procedure in snowflake?

I am working on procedure which would run weekly and delete a week old data from certain tables. Instead of creating task for individual table, I want to pass table names as list/array.

Please guide.

Thanks in advance!

Rosinski answered 26/8, 2020 at 14:5 Comment(0)
M
9

Yes, you can pass an array into a Snowflake JavaScript stored procedure. Here's a sample:

create or replace procedure test(ARR array)
returns string
language javascript
as
$$
    var i; 
    var out = "";

    // Remember to capitalize variables input to the stored procedure definition
    for(i = 0; i < ARR.length; i++){       
        out += ARR[i];
        if (i < ARR.length - 1) out += ", ";
    }
    return out;
$$;


call test(array_construct('TABLE_1', 'TABLE_2', 'TABLE_3'));
Match answered 26/8, 2020 at 14:38 Comment(1)
Thanks.. modified my proc with this for loop..it helped.. :)Rosinski
A
0

I struggled with this for a while, and have come up with a good 1 liner to make an array digestible by a procedure.

myArr = "array_construct('" + MyArr.join("', '") + "')"

this can then be inserted into any call statement like such

var sql_command = `call my_proc(${myArr});`

Hope this helps anyone else who stumbles upon this!

Abixah answered 14/9, 2022 at 19:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.