成品 原料 数量 是否最终原料
A AL 3 否
A AR 4 否 AL AL_1 2 否
AL AL_2 2 否
AL AL_3 3 否 AR AR_1 4 否
AR AR_2 2 否
AR AR_3 3 否 AL_1 AL_11 5 是
AL_2 AL_21 2 是
AL_2 AL_22 3 是
AL_3 AL_31 3 是 AR_1 AR_11 1 是
AR_2 AR_21 2 是
AR_2 AR_22 3 是
AR_3 AR_31 4 是
AR_3 AR_30 4 是 ...可能会有很多层... 求组成A需要最终原料的数量;
结果: 最终原料
AL_11 30
AL_21 12
AL_22 18
AL_31 27 AR_11 16
AR_21 16
AR_22 24
AR_31 ..
AR_30 ..
问题补充:不用临时表
A AL 3 否
A AR 4 否 AL AL_1 2 否
AL AL_2 2 否
AL AL_3 3 否 AR AR_1 4 否
AR AR_2 2 否
AR AR_3 3 否 AL_1 AL_11 5 是
AL_2 AL_21 2 是
AL_2 AL_22 3 是
AL_3 AL_31 3 是 AR_1 AR_11 1 是
AR_2 AR_21 2 是
AR_2 AR_22 3 是
AR_3 AR_31 4 是
AR_3 AR_30 4 是 ...可能会有很多层... 求组成A需要最终原料的数量;
结果: 最终原料
AL_11 30
AL_21 12
AL_22 18
AL_31 27 AR_11 16
AR_21 16
AR_22 24
AR_31 ..
AR_30 ..
问题补充:不用临时表
CREATE FUNCTION dbo.MEnd(@a as varchar(20))
RETURNS varchar(20) AS
BEGIN
while exists(select 成品 from 表 where @a=原料)
select @a=成品 from 表 where @a=原料
return @a
END
select 原料,数量 from 表 where 是否最终原料='是' and dbo.MEnd(原料)='A'
insert into tabb select 'A', 'AL', 3, '否'
union all select 'A', 'AR', 4, '否' union all select 'AL', 'AL_1', 2,'否'
union all select 'AL', 'AL_2', 2, '否'
union all select 'AL', 'AL_3',3, '否'union all select 'AR', 'AR_1', 4, '否'
union all select 'AR', 'AR_2', 2, '否'
union all select 'AR', 'AR_3', 3, '否'union all select 'AL_1', 'AL_11', '5', '是'
union all select 'AL_2', 'AL_21', '2', '是'
union all select 'AL_2', 'AL_22', '3', '是'
union all select 'AL_3', 'AL_31', '3', '是' union all select 'AR_1', 'AR_11', 1, '是'
union all select 'AR_2', 'AR_21', 2, '是'
union all select 'AR_2', 'AR_22', 3, '是'
union all select 'AR_3', 'AR_31', 4, '是'
union all select 'AR_3', 'AR_30', 4, '是' alter FUNCTION dbo.MEnd(@var as varchar(50),@int int)
RETURNS int AS
BEGIN
while exists(select 1 from tabb where 原料=@var)
select @var=成品,@int=@int*数量 from tabb where 原料=@var
return @int
END
select 原料,dbo.MEnd(成品,数量)[数量] from tabb where 是否最终原料='是'原料 数量
-------------------- -----------
AL_11 30
AL_21 12
AL_22 18
AL_31 27
AR_11 16
AR_21 16
AR_22 24
AR_31 48
AR_30 48
insert into bom select
'A', 'AL', 3, 0 union all select
'A', 'AR', 4, 0 union all select
'AL', 'AL_1', 2, 0 union all select
'AL', 'AL_2', 2, 0 union all select
'AL', 'AL_3', 3, 0 union all select
'AR', 'AR_1', 4, 0 union all select
'AR', 'AR_2', 2, 0 union all select
'AR', 'AR_3', 3, 0 union all select
'AL_1', 'AL_11', 5, 1 union all select
'AL_2', 'AL_21', 2, 1 union all select
'AL_2', 'AL_22', 3, 1 union all select
'AL_3', 'AL_31', 3, 1 union all select
'AR_1', 'AR_11', 1, 1 union all select
'AR_2', 'AR_21', 2, 1 union all select
'AR_2', 'AR_22', 3, 1 union all select
'AR_3', 'AR_31', 4, 1 union all select
'AR_3', 'AR_30', 4, 1 CREATE FUNCTION [dbo].[getBom] (@p varchar(8))
RETURNS @tt table(原料 varchar(8),数量 decimal(19,2)) AS
BEGIN
declare @id int,@f varchar(8),@m decimal(19,2)
set @p='A'
declare @t table(id int identity(1,1),成品 varchar(8),原料 varchar(8),数量 decimal(19,2),是否最终原料 bit)
insert into @t select * from bom where 成品=@p
while exists(select * from @t where 是否最终原料=0)
begin
select top 1 @id=id,@f=原料,@m=数量 from @t where 是否最终原料=0
insert into @t select 成品, 原料, 数量*@m, 是否最终原料 from bom where 成品=@f
delete from @t where id=@id
end
insert into @tt select 原料,sum(数量) from @t group by 原料
return
END select * from dbo.getBom('A')原料 数量
-------- ---------------------
AL_11 30.00
AL_21 12.00
AL_22 18.00
AL_31 27.00
AR_11 16.00
AR_21 16.00
AR_22 24.00
AR_30 48.00
AR_31 48.00(所影响的行数为 9 行)
RETURNS int AS
BEGIN
while exists(select 1 from tabb where 原料=@var)
select top 1 @var=成品,@int=@int*数量 from tabb where 原料=@var
return @int
END
那楼主的意思就大了~~也不是一个top 1搞定的了~~