【发布时间】:2019-01-19 07:09:00
【问题描述】:
我编写了一个查询,其中我选择了 2 列,这些列大约有 40 条记录。
现在我想通过一一选择对这些记录进行总和计算。
在我的存储过程中,我使用了一个游标来存储这 40 条记录。并使用 for 循环对这些记录进行总和计算(一一)
但是对于第一条记录,sum 为空,我的循环没有从游标中获取另一条记录。
这就是为什么我无法为每条记录一一计算总和,因为我的 for 循环不再运行。
cursor details is
select distinct lpn_id v_olpn_id, lpn_detail_id v_olpn_detail_id
from lpn_detail ld
where size_value > '0'
and lpn_id in
( select lpn_id from lpn
where lpn_facility_status > '15'
and lpn_facility_status < '90'
and inbound_outbound_indicator = 'O' )
and item_id in
( select item_id from item_cbo where catch_weight_item = '1' )
and not exists
( select 1 from lpn_catch_weight lwc
where lwc.lpn_detail_id = ld.lpn_detail_id );
begin
for detail in details
loop
select sum(ld.ref_num4*td.qty_pulld) into v_total_weight
from lpn_detail ld, lpn l, task_dtl td
where l.lpn_id = ld.lpn_id
and l.tc_lpn_id=td.cntr_nbr
and ld.item_id=td.item_id
and l.lpn_id in
( select lpn_id from lpn where tc_lpn_id in
( select cntr_nbr from task_dtl
where carton_nbr in
( select tc_lpn_id from lpn
where lpn_id=detail.v_olpn_id ) ) )
and ld.ref_num4 is not null
and td.invn_need_type = 60
group by ld.item_id;
end loop;
【问题讨论】:
-
请向我们展示代码!
-
欢迎来到 SO!请重新阅读常见问题解答,edit 您的问题并添加将您的问题转换为minimal reproducible example 的 PL/SQL 代码。
-
提供您尝试过的查询以及示例数据和结果。
-
请检查查询
-
您的外部游标似乎只生成一行,而内部游标没有为
lpn_id找到任何行。这是您的数据存在的问题,而我们没有。
标签: sql oracle for-loop stored-procedures cursor