使用Snowflake中的存储过程从表中检索两列

我尝试使用函数从表中检索数据,并将其存储在数组中。但是当我试图返回两个数组中的一个时,我没有得到任何数据。不知道我哪里出错了。

CREATE OR REPLACE PROCEDURE GET_DATE()
        RETURNS VARCHAR NOT NULL
        LANGUAGE JAVASCRIPT
        AS
        $$
            var array1 = [];
            var array2 = [];
            array1,array2 = get_columns_data(); // want to get those two array values from the function for further use
            return array1; //This is not printing or returning anything

            function get_columns_data()
            {
                var arr1 = [];
                var arr2 = [];
                
                cmdl = {sqlText: `SELECT COUNT(*) AS TOTAL, COUNT(DISTINCT(ID)) AS UNIQUE FROM TABLE1;`} //query to get two column values
                stmt = snowflake.createStatement(cmdl);
                
                var a = stmt.execute();
                while(a.next())
                {
                    var one = a.getColumnValue('TOTAL');
                    arr1.push(one);
                    var two = a.getColumnValue('UNIQUE');
                    arr2.push(two);
                }
                return arr1,arr2;  //returning two arrays; Don't know if this is the correct method to return two arryas
            }
        $$

解决方案

此行在javascript上不起作用:

return arr1,arr2;

相反,您可以返回数组数组:

return [arr1,arr2];

然后您可以接收单个变量中的数组数组:

arrays = get_columns_data();

测试使用逗号返回不起作用,然后通过返回数组修复:

相关文章