本文通過乙個bom表的例子,分別介紹在sql server2000和sql server2005中如何編寫遞迴演算法。
(一) 建立測試資料
createtablebillofmaterial(
productno nvarchar(15), --父元件編號
productname nvarchar(50), --子元件名稱
partno nvarchar(15), --子元件編號
useqty numeric(9,2), --子元件用量
materialclass nchar(1), --元件類, p=成品 m=半成品
unit varchar(10)
--單位
)
insertintobillofmaterial(productno, partno, productname, useqty, materialclass, unit)
select'01651', '01651', n'cable ass''y', null, 'p', 'm'
union
select'01651', '01-1001', n'汽車專用線od=2.3mm', 1, 'm', 's'
union
select
'01651', '01-1002', n'housing白色', 1, 'm', 's'
union
select'01651', '01-1003', n'模組a', 1, 'm', 's'
union
select'01-1003', '01-1004', n'模組a-01', 1, 'm', 's'
union
select'01-1003', '01-1005', n'模組a-02', 1, 'm', 's'
union
select'01-1005', '01-1006', n'模組a', 1, 'm', 's'
union
select'01-1006', '01-1007', n'模組a-01', 1, 'm', 's'
union
select'01-1007', '01-1008', n'模組a-02', 1, 'm', 's'go
(二) sql server2000的實現方法
ifobject_id
('fn_bom_2000'
)is not null
drop
function [fn_bom_2000]go
create
function [dbo].[fn_bom_2000]
(@bo_no nvarchar
(15)
)returns @result table(
line varchar
(255),
partno nvarchar
(15),
partname nvarchar
(20),
useqty float,
materialclass nchar
(1),
[level] int,
[sid] varchar
(255))
asbegin
declare @i int, @restr varchar
(50)
select @i=0, @restr=' ';
insert into @result(line, partno, partname
,useqty, materialclass, [level], [sid])
select productno, partno, productname
,1, materialclass, @i, @bo_no
from billofmaterial
where
productno=@bo_noanduseqtyis null
while
@@rowcount>0
begin
set @i=@i+1
insert
into @result(line, partno, partname
,useqty, materialclass, [level], [sid])
select
substring
(left
(@restr,@i)
+'├----------------------------',1,8)
,b.partno,b.productname
,b.useqty*r.useqty, b.materialclass,@i, r.sid+'_'+b.partno
from billofmaterial b, @result as r
where r.[level]=@i-1
and b.productno=r.partno
and b.useqty>0
end
return
endgo
select * from dbo.fn_bom_2000(
'01651')
(三) sql server2005的實現方法
ifobject_id
('fn_bom_2005'
)is not null
drop
function [fn_bom_2005]go
create
function [dbo].[fn_bom_2005]
( @bo_no nvarchar
(15)
)returns @result table
(line nvarchar
(255),
partno nvarchar
(15),
partname nvarchar
(20),
useqty float,
materialclass nchar
(1),
level int,
sid nvarchar(255))
asbegin
declare @restr varchar
(50)
set @restr=' ';
with t(line,partno, partname, useqty, materialclass, [level], [sid])as(
select
cast
(productno as
varchar(255))
,partno
,productname
,convert
(float,1.0)
,materialclass
,0,cast(@bo_no as
varchar
(255))
from billofmaterial
where
productno=@bo_noanduseqtyis null
union all
select
cast
(substring
(left
(@restr, t.[level]+1)
+'|------------------', 1, 8)
as varchar(255))
,r.partno
,r.productname
,convert
(float,t.useqty*r.useqty)
,r.materialclass
,t.[level]+1 as [level]
,cast
(t.[sid]+'_'+r.partno as varchar(255))
as [sid]
from billofmaterial rinner joint
on r.productno=t.partno and r.useqty>0
)insert
into @result select * from t
return
endgo
select * from dbo.fn_bom_2005(
'01651')
sql 實現遞迴演算法
根據傳入引數查詢指定項的子項的遞迴用法 樣表 child,parent 1 0 2 0 3 1 4 2 要 實現這種用法一般都通過兩種方式來實現 procedure 方式 create procedure usp getallchild child int asdeclare t table chi...
求SQL語句遞迴的演算法
表結構是這樣的 部門 上級部門 a b b c c d a a b b c c 求一條sql語句,根據a查其上級部門,查詢結果為 上級部門bc d用函式 create table tb 部門 varchar 20 上級部門 varchar 20 insert into tb select a b u...
用sql語句彙總 實現語句的合併
待驗收入庫單 create table t cw warehousing entry bosid number not null,wareid varchar2 50 入庫單編號 stocktime varchar2 50 採購時間 empid varchar2 50 登記人 cw addr var...