Код: Выделить всё
execute block
as
declare variable fk_tov integer;
declare variable stock type of column bd_11.stock;
declare variable max_rash_date date;
begin
for select bd_tovar.field_key, sum(bd_11.stock)
from bd_tovar left join bd_11 on bd_11.fk_bd_tovar=bd_tovar.field_key
where bd_tovar.is_old_flag = 0
group by 1
having sum(bd_11.stock) = 0
into :fk_tov, :stock
do
begin
select max(bd_21.date_rash) from bd_21 where fk_bd_tovar = :fk_tov into :max_rash_date;
max_rash_date = coalesce(:max_rash_date,dateadd(year, -1, current_date));
if (max_rash_date<dateadd(month, -3, current_date)) then
begin
update bd_tovar set is_old_flag=1 where field_key = :fk_tov;
end
end
end