admin 管理员组文章数量: 1086019
I am trying to load the data from json to structured file and getting below error :
092234 (P0000): Uncaught exception of type 'STATEMENT_ERROR' on line 35 at position 8 : Expression in variant for loop does not evaluate to an array or an object
Below is the procedure
CREATE OR REPLACE PROCEDURE parse_json_and_load_output()
RETURNS STRING
LANGUAGE SQL
EXECUTE AS CALLER
AS
$$
DECLARE
-- Variables to hold the JSON data from the input table
input_json VARIANT;
-- Variables to hold values for each field
company_name STRING;
fiscal_year ARRAY;
gross_profit ARRAY;
net_income ARRAY;
fiscal_year_item VARIANT;
gross_profit_item VARIANT;
net_income_item VARIANT;
counter int;
-- Temporary variable to hold the result of each row
result STRING;
-- Cursor to loop through the input table rows
input_cursor CURSOR FOR
SELECT json_content
FROM temp_table;
BEGIN
-- Iterate over each row in the input table
FOR var IN input_cursor DO
input_json := var.json_content;
-- Extract values from the JSON object
company_name := input_json:company_name[0].value::STRING;
fiscal_year := input_json:Fiscal_Year;
gross_profit := input_json:Gross_profit;
net_income := input_json:Net_income;
counter:=0;
FOR i IN ARRAY_SIZE(fiscal_year) - 1 DO
fiscal_year_item := fiscal_year[i];
gross_profit_item := gross_profit[i];
net_income_item := net_income[i];
-- Insert the parsed values into the output_table
INSERT INTO tgt_temp (company_name, fiscal_year, gross_Profit, net_income)
VALUES (
company_name,
fiscal_year_item.value::STRING,
gross_profit_item.value::STRING,
net_income_item.value::STRING
);
counter:=counter+1;
END FOR;
END FOR;
-- Return a success message
RETURN 'Data loaded successfully into output_table.';
END;
$$;
Please help me to fix this.
Thanks much!
I am trying to load the data from json to structured file and getting below error :
092234 (P0000): Uncaught exception of type 'STATEMENT_ERROR' on line 35 at position 8 : Expression in variant for loop does not evaluate to an array or an object
Below is the procedure
CREATE OR REPLACE PROCEDURE parse_json_and_load_output()
RETURNS STRING
LANGUAGE SQL
EXECUTE AS CALLER
AS
$$
DECLARE
-- Variables to hold the JSON data from the input table
input_json VARIANT;
-- Variables to hold values for each field
company_name STRING;
fiscal_year ARRAY;
gross_profit ARRAY;
net_income ARRAY;
fiscal_year_item VARIANT;
gross_profit_item VARIANT;
net_income_item VARIANT;
counter int;
-- Temporary variable to hold the result of each row
result STRING;
-- Cursor to loop through the input table rows
input_cursor CURSOR FOR
SELECT json_content
FROM temp_table;
BEGIN
-- Iterate over each row in the input table
FOR var IN input_cursor DO
input_json := var.json_content;
-- Extract values from the JSON object
company_name := input_json:company_name[0].value::STRING;
fiscal_year := input_json:Fiscal_Year;
gross_profit := input_json:Gross_profit;
net_income := input_json:Net_income;
counter:=0;
FOR i IN ARRAY_SIZE(fiscal_year) - 1 DO
fiscal_year_item := fiscal_year[i];
gross_profit_item := gross_profit[i];
net_income_item := net_income[i];
-- Insert the parsed values into the output_table
INSERT INTO tgt_temp (company_name, fiscal_year, gross_Profit, net_income)
VALUES (
company_name,
fiscal_year_item.value::STRING,
gross_profit_item.value::STRING,
net_income_item.value::STRING
);
counter:=counter+1;
END FOR;
END FOR;
-- Return a success message
RETURN 'Data loaded successfully into output_table.';
END;
$$;
Please help me to fix this.
Thanks much!
Share Improve this question edited Mar 28 at 17:53 samhita 4,0802 gold badges11 silver badges18 bronze badges asked Mar 28 at 14:52 BalajiAWSBalajiAWS 1371 gold badge3 silver badges15 bronze badges 2 |1 Answer
Reset to default 1I tried to reproduce your issue with sample data
There are a couple of points to note here -
1.In the For loop
FOR i IN ARRAY_SIZE(fiscal_year) - 1 DO
If I understand correctly above is a Counter based FOR loop, so you need to give a start point as mentioned in documentation
FOR <counter_variable> IN [ REVERSE ] <start> TO <end> { DO | LOOP }
<statement>;
[ <statement>; ... ]
END { FOR | LOOP } [ <label> ] ;
so above statement becomes
FOR i IN 0 TO ARRAY_SIZE(fiscal_year) - 1 DO --I have started with 0 but you can use any start point
Once you fix above you are going to encounter further issues as I highlighted below -
2.In the INSERT statement since you are using variables to insert , it should be :fiscal_year_item
instead of fiscal_year_item
, note the colon (:
).
Here is a sample json I used
{
"Fiscal_Year": [
2020,
2021
],
"Gross_profit": [
50000,
60000
],
"Net_income": [
30000,
40000
],
"company_name": "A"
}
Here is the procedure as per above data(Tables are in Test.test schema, you can change accordingly)
CREATE OR REPLACE PROCEDURE parse_json_and_load_output()
RETURNS STRING
LANGUAGE SQL
EXECUTE AS CALLER
AS
$$
DECLARE
-- Variables to hold the JSON data from the input table
input_json VARIANT;
-- Variables to hold values for each field
company_name STRING;
fiscal_year ARRAY;
gross_profit ARRAY;
net_income ARRAY;
fiscal_year_item STRING;
gross_profit_item STRING;
net_income_item STRING;
counter int;
-- Temporary variable to hold the result of each row
result STRING;
-- Cursor to loop through the input table rows
input_cursor CURSOR FOR
SELECT json_content
FROM test.test.temp_table;
BEGIN
-- Iterate over each row in the input table
FOR var IN input_cursor DO
input_json := var.json_content;
-- Extract values from the JSON object
company_name := input_json:company_name::STRING;
fiscal_year := input_json:Fiscal_Year;
gross_profit := input_json:Gross_profit;
net_income := input_json:Net_income;
counter:=0;
FOR i IN 0 to ARRAY_SIZE(fiscal_year) - 1 DO
fiscal_year_item := fiscal_year[i];
gross_profit_item := gross_profit[i];
net_income_item := net_income[i];
INSERT INTO test.test.tgt_temp ( company_name,fiscal_year
, gross_Profit, net_income)
VALUES (
:company_name,
:fiscal_year_item::STRING,
:gross_profit_item::STRING,
:net_income_item::STRING
);
counter:=counter+1;
END FOR;
END FOR;
-- Return a success message
RETURN 'Data loaded successfully into output_table.';
END;
$$;
Upon calling the procedure
call parse_json_and_load_output() ;
Another point is you are not using counter anywhere, do you need it?
本文标签:
版权声明:本文标题:stored procedures - Uncaught exception of type 'STATEMENT_ERROR' on line 35 at position 8 : Expression in varian 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1744030273a2521368.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
SELECT json_content FROM temp_table;
You need to share sample sample data for this – samhita Commented Mar 28 at 17:37