使用plpgsql在空数组内附加值数组

时间:2019-05-26 04:20:26

标签: postgresql plpgsql

我有一个Postgres函数,其中选择查询返回数组中的结果。我需要循环来自选择查询的结果数组并将各个数组值附加到空数组中。我尝试使用下面的代码。但是,仅select查询的第一行会被追加到空数组。

st_codes text[];
st_codes = {'AZ','PA'}
begin
FOR states in 1 .. array_upper(st_codes, 1) LOOP
        records = '{}'; /*making records array to empty*/
        select array[year,month_availability] from data_availability where year_availability = 1 and state_code = '|| st_codes[states]||' order by year desc into records; /*returns 3 rows in an integer array when running this query separately usinga state code*/ {{2017,12},{2016,12},{2015,12}}

        raise notice 'Value: %', records; /*am getting only {2017,12} instead of {{2017,12},{2016,12},{2015,12}}

END LOOP;
end

0 个答案:

没有答案