| 发表于:2007-09-03 14:33:551楼 得分:10 |
找個例子給你吧 create table ps_mstr(ps_par varchar(10)collate latin1_general_bin,ps_comp varchar(10),qty numeric(9,2) ) --sp_help 'ps_mstr ' insert into ps_mstr select 'fg001 ', 'sfg001 ', 1 union all select 'fg001 ' , 'sfg002 ', 1 union all select 'fg001 ' , 'sfg003 ', 1 union all select 'sfg001 ', 'wip001 ', 2 union all select 'sfg001 ' , 'wip002 ', 2 union all select 'sfg002 ' , 'wip003 ', 3 union all select 'sfg002 ' , 'wip004 ', 3 union all select 'sfg002 ' , 'wip005 ', 2 union all select 'sfg003 ' , 'wip006 ', 3 union all select 'wip001 ' , 'raw001 ', 2.66 union all select 'wip001 ' , 'raw002 ' , 2.33 union all select 'wip002 ' , 'raw003 ' , 3.21 union all select 'wip003 ' , 'raw004 ' , 1.89 union all select 'wip003 ' , 'raw005 ' , 1.86 create function f_cid(@ps_par varchar(10)) returns @t_level table(ps_par varchar(10)collate latin1_general_bin,ps_comp varchar(10),qty numeric(9,2),level int) as begin declare @level int set @level=1 insert into @t_level select ps_par,ps_comp,qty,@level from ps_mstr where ps_par=@ps_par collate latin1_general_bin while @@rowcount> 0 begin set @level=@level+1 insert into @t_level select a.ps_par, a.ps_comp,a.qty*b.qty,@level from ps_mstr a,@t_level b where a.ps_par=b.ps_comp collate latin1_general_bin--(秶俶齬唗) and b.level=@level-1 end return end go select * from f_cid( 'fg001 ') | | |
|