pl/sql 的递归,哈哈
declare
i number;
j number;
t1 bom.bom_inventory_components.item_num%type;
t2 bom.bom_inventory_components.component_quantity%type;
t3 bom.bom_inventory_components.supply_subinventory%type;
t4 inv.mtl_system_items_b.segment1%type;
t5 inv.mtl_system_items_b.description%type;
prod_item_id number;
prod_item_name inv.mtl_system_items_b.segment1%type;
prod_item_desc inv.mtl_system_items_b.description%type;
father_item_id number;
type arr is record (css number);
TYPE type_tab IS TABLE OF arr INDEX BY BINARY_INTEGER;
t type_tab;
item type_tab;
cursor c is select bic.component_item_id,
bic.item_num,
bic.component_quantity,
bic.supply_subinventory,
msi.segment1,
msi.description
from bom.bom_bill_of_materials bbom,
bom.bom_inventory_components bic,
inv.mtl_system_items_b msi
where bbom.organization_id = 32
and bic.disable_date is null
and bbom.bill_sequence_id = bic.bill_sequence_id
and bic.component_item_id = msi.inventory_item_id
and bbom.organization_id = msi.organization_id
and bbom.assembly_item_id = father_item_id
order by bic.item_num;
begin
select a.inventory_item_id, a.segment1, a.description
into prod_item_id, prod_item_name, prod_item_desc
from inv.mtl_system_items_b a
where a.organization_id = 32
and a.segment1 = '&产品物料编码';
dbms_output.put_line('产品名称: '||prod_item_name||' '||'产品说明: '||prod_item_desc); --OUTPUT产品
dbms_output.put_line('');
dbms_output.put_line(' 组件'||' '||'物料说明'||' '||'序号 '||'数量 '||'子库存');
father_item_id := prod_item_id;
dbms_output.put_line('----------------------------------------------------------------------------------------------------');
for i in 0..9 loop
t(i).css := 0;
item(i).css := 0;
end loop;
i := 1;
item(1).css := prod_item_id;
while t(0).css=0 loop
open c;
for j in 0..t(i).css loop
fetch c into father_item_id,t1,t2,t3,t4,t5;
if c%notfound then
father_item_id := 0;
else
item(i+1).css := father_item_id;
end if;
end loop;
if father_item_id=0 then
t(i).css := 0;
i := i -1;
t(i).css := t(i).css + 1;
father_item_id := item(i).css;
else
dbms_output.put_line(lpad(' ',i*2,' ')||'|-'||rpad(t4,40-(i*2+2),' ')||rpad(t5,40,' ')||rpad(t1,6,' ')||rpad(t2,6,' ')||t3);
i := i + 1;
end if;
close c;
end loop;
end;