+ -
当前位置:首页 → 问答吧 → 物料管理 两张表 递归(等待)

物料管理 两张表 递归(等待)

时间:2011-09-23

来源:互联网

bom1表结构
bom单号 物料
00001 水壶
00002 壶身

bom2表结构
bom单号 物料 使用量
00001 水壶盖 1
00001 壶身 3
00001 壶底 1
00002 铁皮 1
00002 螺丝 2

生成水壶 我需要:壶盖、壶底、螺丝、铁皮
怎么使用oracle的递归查出来

就是先看通过bom1的物料得到bom单号,再通过bom单号去bom2查物料,再将这个物料放到bom1中查看是否有bom单号,有的话再通过bom单号去bom2查物料……

我昨天写了一个不对只能查顶层一个,忘大家帮忙

create or replace function Fun_Bom(name in varchar) return number is
  Result number;
  id varchar2(20); 
  cursor my_cursor is
  select 物料 from bom2 where bom单号 = id;
  mat bom2.物料%type;
begin
  select max(nvl(bom单号, '')) into id from bom1 where 物料=name;
  if id is not null then  
  dbms_output.put_line(id);
  open my_cursor;
  loop
  fetch my_cursor into mat;
  DBMS_OUTPUT.PUT_LINE(mat);
  return Fun_Bom(mat);
  exit when my_cursor%notfound;  
  end loop;
  close my_cursor; 
  end if; 
  return(0);
end Fun_Bom;

作者: one_in_one   发布时间: 2011-09-23

oracle的不熟 不过我给个SQL Server的思路吧



SQL code


while @结果 = 1 
begin
  if @需要查询的表 = 0 
  begin
    在表一中查
     set  @结果 =  @@rowcount
    set  @需要查询的表 = 1;
  end
  else
  begin
    在表二中查
     set  @结果 =  @@rowcount
    set  @需要查询的表 = 0;
  end
end


作者: warrially   发布时间: 2011-09-23

谢谢!Oracle里的特有的函数我也不会用,我用的对多的也是SQL Server

作者: one_in_one   发布时间: 2011-09-23

热门下载

更多