将多列结果查询存储在数组变量中
Store multiple columns result query in array variable
我想将查询结果存储在自定义类型的数组列表中。
创建的类型为:
create type statement_part AS (
statementid integer,
statement_value_date timestamp,
statement_last_transmission timestamp);
程序代码:
CREATE OR REPLACE FUNCTION public.get_bank_statements_downloadable_state
(
folderID integer,
date1 timestamp without time zone,
date2 timestamp without time zone
)
RETURNS bank_day[]
LANGUAGE plpgsql
AS $get_bank_statements_downloadable_state$
declare
statements_part_array statement_part[];
temp_statements_part_array statement_part[];
statements_part_element statement_part;
bank_day_array bank_day[];
bank_day_element bank_day;
nb_days integer;
begin
select array(bast_id::integer), array(bast_valuedate), array(bast_lasttransmission)
from bast_bankstatement
inner join baac_bankaccount
on bast_baac_id = baac_bankaccount.baac_id
where baac_folder_id = folderid
and (bast_valuedate between date1 and date2)
and bast_state = 1
into statements_part_array;
foreach statements_part_element in array statements_part_array loop
RAISE NOTICE 'Array list(%)', statements_part_array;
end loop;
nb_days := (select * from DATE_PART('day', AGE(date1, date2)) AS days);
for dayCounter in 0..nb_days-1 loop
--on met dans notre tableau temporaire les relevés banque valides du jour du cabinet
temp_statements_part_array :=
(
select *
from statements_part_array
where statements_part_array.statement_value_date = date1+dayCounter
);
if count(temp_statement_list) = 0 then
bank_day_element := (date1+dayCounter,0);
else
bank_day_element := (date1+dayCounter,2);
foreach statements_part_element in array temp_statements_part_array
loop
if statements_part_element.statement_last_transmission is null then
bank_day_element := (date1+dayCounter,1);
EXIT;
end if;
end loop;
end if;
bank_day_array := array_append(bank_day_array,bank_day_element);
end loop;
return bank_day_array;
END;
$get_bank_statements_downloadable_state$;
但是失败了。它说子查询在我执行时必须 return 只有一列。
我使用
解决了这个问题
select array(select row(bast_id,bast_valuedate,bast_lasttransmission)
into statements_part_array
from bast_bankstatement
inner join baac_bankaccount
on bast_baac_id = baac_bankaccount.baac_id
where baac_folder_id = folderid
and (bast_valuedate between date1 and date2)
and bast_state = 1)
as statements_part_array;
我想将查询结果存储在自定义类型的数组列表中。
创建的类型为:
create type statement_part AS (
statementid integer,
statement_value_date timestamp,
statement_last_transmission timestamp);
程序代码:
CREATE OR REPLACE FUNCTION public.get_bank_statements_downloadable_state
(
folderID integer,
date1 timestamp without time zone,
date2 timestamp without time zone
)
RETURNS bank_day[]
LANGUAGE plpgsql
AS $get_bank_statements_downloadable_state$
declare
statements_part_array statement_part[];
temp_statements_part_array statement_part[];
statements_part_element statement_part;
bank_day_array bank_day[];
bank_day_element bank_day;
nb_days integer;
begin
select array(bast_id::integer), array(bast_valuedate), array(bast_lasttransmission)
from bast_bankstatement
inner join baac_bankaccount
on bast_baac_id = baac_bankaccount.baac_id
where baac_folder_id = folderid
and (bast_valuedate between date1 and date2)
and bast_state = 1
into statements_part_array;
foreach statements_part_element in array statements_part_array loop
RAISE NOTICE 'Array list(%)', statements_part_array;
end loop;
nb_days := (select * from DATE_PART('day', AGE(date1, date2)) AS days);
for dayCounter in 0..nb_days-1 loop
--on met dans notre tableau temporaire les relevés banque valides du jour du cabinet
temp_statements_part_array :=
(
select *
from statements_part_array
where statements_part_array.statement_value_date = date1+dayCounter
);
if count(temp_statement_list) = 0 then
bank_day_element := (date1+dayCounter,0);
else
bank_day_element := (date1+dayCounter,2);
foreach statements_part_element in array temp_statements_part_array
loop
if statements_part_element.statement_last_transmission is null then
bank_day_element := (date1+dayCounter,1);
EXIT;
end if;
end loop;
end if;
bank_day_array := array_append(bank_day_array,bank_day_element);
end loop;
return bank_day_array;
END;
$get_bank_statements_downloadable_state$;
但是失败了。它说子查询在我执行时必须 return 只有一列。
我使用
解决了这个问题 select array(select row(bast_id,bast_valuedate,bast_lasttransmission)
into statements_part_array
from bast_bankstatement
inner join baac_bankaccount
on bast_baac_id = baac_bankaccount.baac_id
where baac_folder_id = folderid
and (bast_valuedate between date1 and date2)
and bast_state = 1)
as statements_part_array;