参考: /* 标题:查询指定节点及其所有子节点的函数 作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开) 时间:2008-05-12 地点:广东深圳 */create table tb(id varchar(3) , pid varchar(3) , name varchar(10)) insert into tb values('001' , null , '广东省') insert into tb values('002' , '001' , '广州市') insert into tb values('003' , '001' , '深圳市') insert into tb values('004' , '002' , '天河区') insert into tb values('005' , '003' , '罗湖区') insert into tb values('006' , '003' , '福田区') insert into tb values('007' , '003' , '宝安区') insert into tb values('008' , '007' , '西乡镇') insert into tb values('009' , '007' , '龙华镇') insert into tb values('010' , '007' , '松岗镇') go--查询指定节点及其所有子节点的函数 create function f_cid(@ID varchar(3)) returns @t_level table(id varchar(3) , level int) as begin declare @level int set @level = 1 insert into @t_level select @id , @level while @@ROWCOUNT > 0 begin set @level = @level + 1 insert into @t_level select a.id , @level from tb a , @t_Level b where a.pid = b.id and b.level = @level - 1 end return end go--调用函数查询001(广东省)及其所有子节点 select a.* from tb a , f_cid('001') b where a.id = b.id order by a.id /* id pid name ---- ---- ---------- 001 NULL 广东省 002 001 广州市 003 001 深圳市 004 002 天河区 005 003 罗湖区 006 003 福田区 007 003 宝安区 008 007 西乡镇 009 007 龙华镇 010 007 松岗镇(所影响的行数为 10 行) */--调用函数查询002(广州市)及其所有子节点 select a.* from tb a , f_cid('002') b where a.id = b.id order by a.id /* id pid name ---- ---- ---------- 002 001 广州市 004 002 天河区(所影响的行数为 2 行) */--调用函数查询003(深圳市)及其所有子节点 select a.* from tb a , f_cid('003') b where a.id = b.id order by a.id /* id pid name ---- ---- ---------- 003 001 深圳市 005 003 罗湖区 006 003 福田区 007 003 宝安区 008 007 西乡镇 009 007 龙华镇 010 007 松岗镇(所影响的行数为 7 行) */drop table tb drop function f_cid/* 标题:查询指定节点及其所有父节点的函数 作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开) 时间:2008-05-12 地点:广东深圳 */create table tb(id varchar(3) , pid varchar(3) , name varchar(10)) insert into tb values('001' , null , '广东省') insert into tb values('002' , '001' , '广州市') insert into tb values('003' , '001' , '深圳市') insert into tb values('004' , '002' , '天河区') insert into tb values('005' , '003' , '罗湖区') insert into tb values('006' , '003' , '福田区') insert into tb values('007' , '003' , '宝安区') insert into tb values('008' , '007' , '西乡镇') insert into tb values('009' , '007' , '龙华镇') insert into tb values('010' , '007' , '松岗镇') go--查询指定节点及其所有父节点的函数 create function f_pid(@id varchar(3)) returns @t_level table(id varchar(3)) as begin insert into @t_level select @id select @id = pid from tb where id = @id and pid is not null while @@ROWCOUNT > 0 begin insert into @t_level select @id select @id = pid from tb where id = @id and pid is not null end return end go--调用函数查询002(广州市)及其所有父节点 select a.* from tb a , f_pid('002') b where a.id = b.id order by a.id /* id pid name ---- ---- ---------- 001 NULL 广东省 002 001 广州市(所影响的行数为 2 行) */--调用函数查询003(深圳市)及其所有父节点 select a.* from tb a , f_pid('003') b where a.id = b.id order by a.id /* id pid name ---- ---- ---------- 001 NULL 广东省 003 001 深圳市(所影响的行数为 2 行) */--调用函数查询008(西乡镇)及其所有父节点 select a.* from tb a , f_pid('008') b where a.id = b.id order by a.id /* id pid name ---- ---- ---------- 001 NULL 广东省 003 001 深圳市 007 003 宝安区 008 007 西乡镇(所影响的行数为 4 行) */drop table tb drop function f_pid
CREATE TABLE BOM(PID INT,ID INT) INSERT INTO BOM SELECT 801,101 INSERT INTO BOM SELECT 801,102 INSERT INTO BOM SELECT 801,103 INSERT INTO BOM SELECT 801,601 INSERT INTO BOM SELECT 601,101 INSERT INTO BOM SELECT 601,105 INSERT INTO BOM SELECT 601,501 INSERT INTO BOM SELECT 501,106 INSERT INTO BOM SELECT 501,121 GOCREATE FUNCTION F_GETROOT(@PID INT) RETURNS INT AS BEGIN DECLARE @ID INT WHILE EXISTS(SELECT 1 FROM BOM WHERE ID=@PID) BEGIN SET @ID=@PID SELECT @PID=PID FROM BOM WHERE ID=@ID END RETURN @PID END GOSELECT PID=DBO.F_GETROOT(PID),ID FROM BOM GO/* PID ID ----------- ----------- 801 101 801 102 801 103 801 601 801 101 801 105 801 501 801 106 801 121 */ DROP FUNCTION F_GETROOT DROP TABLE BOM GO --生成测试数据 create table BOM_1(Item int,bom_head varchar(20),bom_child varchar(20),number int,products_attribute varchar(20)) insert into BOM_1 select 1 ,'A' ,'A1',1,'采购' insert into BOM_1 select 2 ,'A' ,'A2',2,'生产' insert into BOM_1 select 3 ,'A2','A3',3,'生产' insert into BOM_1 select 4 ,'A2','A4',2,'采购' insert into BOM_1 select 5 ,'A3','A5',2,'采购' insert into BOM_1 select 6 ,'A3','A6',1,'采购' insert into BOM_1 select 7 ,'B' ,'B1',1,'采购' insert into BOM_1 select 8 ,'B' ,'B2',2,'生产' insert into BOM_1 select 9 ,'B2','B3',3,'生产' insert into BOM_1 select 10,'B2','B4',2,'采购' insert into BOM_1 select 11,'B3','B5',2,'采购' insert into BOM_1 select 12,'B3','B6',2,'采购' go --创建用户定义函数,用于取每个父节点下子节点的采购配置信息 create function f_stock(@bom_head varchar(20)) returns @t table(bom varchar(20),number int) as begin declare @level int declare @a table(bom varchar(20),number int,products_attribute varchar(20),[level] int) set @level=1 if exists(select 1 from BOM_1 where bom_head=@bom_head) insert into @a select bom_child,number,products_attribute,@level from BOM_1 where bom_head=@bom_head
while exists(select 1 from @a where [level]=@level and products_attribute='生产') begin set @level=@level+1 insert into @a(bom,number,products_attribute,[level]) select a.bom_child,a.number,a.products_attribute,@level from BOM_1 a,@a b where a.bom_head=b.bom and b.[level]=@level-1 end
insert into @t(bom,number) select bom,number from @a where products_attribute='采购' return end go --执行调用,取父节点'A'一个标准配置分解的采购信息及数量 select * from dbo.f_stock('A') --生成测试数据 create table BOM(ID INT,PID INT,MSG VARCHAR(1000)) insert into BOM select 1,0,NULL insert into BOM select 2,1,NULL insert into BOM select 3,1,NULL insert into BOM select 4,2,NULL insert into BOM select 5,3,NULL insert into BOM select 6,5,NULL insert into BOM select 7,6,NULL go--创建用户定义函数用于取每个父节点下子节点的采购配置信息 create function f_getChild(@ID VARCHAR(10)) returns @t table(ID VARCHAR(10),PID VARCHAR(10),Level INT) as begin declare @i int set @i = 1 insert into @t select ID,PID,@i from BOM where PID = @ID
while @@rowcount<>0 begin set @i = @i + 1
insert into @t select a.ID,a.PID,@i from BOM a,@t b where a.PID=b.ID and b.Level = @i-1 end return end go--执行查询 select ID from dbo.f_getChild(3) go--输出结果 /* ID ---- 5 6 7 */--删除测试数据 drop function f_getChild drop table BOM创建用户定义函数,每个子节点de父节点的信息 --生成测试数据 create table BOM(ID int,parentID int,sClassName varchar(10)) insert into BOM values(1,0,'1111' ) insert into BOM values(2,1,'1111_1' ) insert into BOM values(3,2,'1111-1-1' ) insert into BOM values(4,3,'1111-1-1-1') insert into BOM values(5,1,'1111-2' )go--创建用户定义函数,每个子节点de父节点的信息 create function f_getParent(@ID int) returns varchar(40) as begin declare @ret varchar(40) while exists(select 1 from BOM where ID=@ID and parentID<>0) begin select @ID=b.ID,@ret=','+rtrim(b.ID)+isnull(@ret,'') from BOM a,BOM b where a.ID=@ID and b.ID=a.parentID end
set @ret=stuff(@ret,1,1,'') return @ret end go--执行查询 select ID,isnull(dbo.f_getParent(ID),'') as parentID from BOM go--输出结果 /* ID parentID ----------- ---------------------------------------- 1 2 1 3 1,2 4 1,2,3 5 1 */--删除测试数据 drop function f_getParent drop table BOM go
近日有其他部门需要我部提供公司当月成品库存折算成原料的数量,故此需要实现把所有成品n层的BOM表全部展开为一层。其中同事给出的一个方案极其简便,其原理即是:1.用递归算法对某个成品料号进行循环拆分,直到拆分标记为空时跳出;2.把该语句设为一个自定义函数,以方便在存储中调用;3.使用游标依次把需要拆分的成品料号赋值给上面所建函数。其中前两步语句如下:Create Function TestBOM(@bono varchar(20)) Returns @TestTable Table(bono varchar(20),elno Varchar(20),boqty decimal(24,4),level int,t int) As Begin declare @level int set @level=1
---原料、成品、半成品全部写入临时表 Insert into @TestTable select bono,elno,round(boqty/bounit*(1+elloss),4) as boqty,@level,0 from bomtable nolock where bono =@bono ----成品半成品设置t值为1,只有是1的才进行折分 update @TestTable set t=1 where elno in (select bono from bomtable nolock) and level=@level while @@rowcount>0 begin set @level=@level+1
Insert into @TestTable select a.bono,a.elno,round(a.boqty/a.bounit*(1+a.elloss),4) as boqty,@level,0 from bomtable a(nolock) ,@TestTable b where a.bono =b.elno and b.level=@level-1 and b.t=1 ----将半成品的 t 设为1 加以区分,只有是1的才进行折分 update @TestTable set t=1 where elno in (select bono from bomtable nolock) and level=@level end return end GO 至此,根据需要把指定料号赋值给此函数即可进行拆分。
/*
标题:查询指定节点及其所有子节点的函数
作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开)
时间:2008-05-12
地点:广东深圳
*/create table tb(id varchar(3) , pid varchar(3) , name varchar(10))
insert into tb values('001' , null , '广东省')
insert into tb values('002' , '001' , '广州市')
insert into tb values('003' , '001' , '深圳市')
insert into tb values('004' , '002' , '天河区')
insert into tb values('005' , '003' , '罗湖区')
insert into tb values('006' , '003' , '福田区')
insert into tb values('007' , '003' , '宝安区')
insert into tb values('008' , '007' , '西乡镇')
insert into tb values('009' , '007' , '龙华镇')
insert into tb values('010' , '007' , '松岗镇')
go--查询指定节点及其所有子节点的函数
create function f_cid(@ID varchar(3)) returns @t_level table(id varchar(3) , level int)
as
begin
declare @level int
set @level = 1
insert into @t_level select @id , @level
while @@ROWCOUNT > 0
begin
set @level = @level + 1
insert into @t_level select a.id , @level
from tb a , @t_Level b
where a.pid = b.id and b.level = @level - 1
end
return
end
go--调用函数查询001(广东省)及其所有子节点
select a.* from tb a , f_cid('001') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
001 NULL 广东省
002 001 广州市
003 001 深圳市
004 002 天河区
005 003 罗湖区
006 003 福田区
007 003 宝安区
008 007 西乡镇
009 007 龙华镇
010 007 松岗镇(所影响的行数为 10 行)
*/--调用函数查询002(广州市)及其所有子节点
select a.* from tb a , f_cid('002') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
002 001 广州市
004 002 天河区(所影响的行数为 2 行)
*/--调用函数查询003(深圳市)及其所有子节点
select a.* from tb a , f_cid('003') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
003 001 深圳市
005 003 罗湖区
006 003 福田区
007 003 宝安区
008 007 西乡镇
009 007 龙华镇
010 007 松岗镇(所影响的行数为 7 行)
*/drop table tb
drop function f_cid/*
标题:查询指定节点及其所有父节点的函数
作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开)
时间:2008-05-12
地点:广东深圳
*/create table tb(id varchar(3) , pid varchar(3) , name varchar(10))
insert into tb values('001' , null , '广东省')
insert into tb values('002' , '001' , '广州市')
insert into tb values('003' , '001' , '深圳市')
insert into tb values('004' , '002' , '天河区')
insert into tb values('005' , '003' , '罗湖区')
insert into tb values('006' , '003' , '福田区')
insert into tb values('007' , '003' , '宝安区')
insert into tb values('008' , '007' , '西乡镇')
insert into tb values('009' , '007' , '龙华镇')
insert into tb values('010' , '007' , '松岗镇')
go--查询指定节点及其所有父节点的函数
create function f_pid(@id varchar(3)) returns @t_level table(id varchar(3))
as
begin
insert into @t_level select @id
select @id = pid from tb where id = @id and pid is not null
while @@ROWCOUNT > 0
begin
insert into @t_level select @id select @id = pid from tb where id = @id and pid is not null
end
return
end
go--调用函数查询002(广州市)及其所有父节点
select a.* from tb a , f_pid('002') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
001 NULL 广东省
002 001 广州市(所影响的行数为 2 行)
*/--调用函数查询003(深圳市)及其所有父节点
select a.* from tb a , f_pid('003') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
001 NULL 广东省
003 001 深圳市(所影响的行数为 2 行)
*/--调用函数查询008(西乡镇)及其所有父节点
select a.* from tb a , f_pid('008') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
001 NULL 广东省
003 001 深圳市
007 003 宝安区
008 007 西乡镇(所影响的行数为 4 行)
*/drop table tb
drop function f_pid
INSERT INTO BOM SELECT 801,101
INSERT INTO BOM SELECT 801,102
INSERT INTO BOM SELECT 801,103
INSERT INTO BOM SELECT 801,601
INSERT INTO BOM SELECT 601,101
INSERT INTO BOM SELECT 601,105
INSERT INTO BOM SELECT 601,501
INSERT INTO BOM SELECT 501,106
INSERT INTO BOM SELECT 501,121
GOCREATE FUNCTION F_GETROOT(@PID INT)
RETURNS INT
AS
BEGIN
DECLARE @ID INT
WHILE EXISTS(SELECT 1 FROM BOM WHERE ID=@PID)
BEGIN
SET @ID=@PID
SELECT @PID=PID FROM BOM WHERE ID=@ID
END
RETURN @PID
END
GOSELECT PID=DBO.F_GETROOT(PID),ID FROM BOM
GO/*
PID ID
----------- -----------
801 101
801 102
801 103
801 601
801 101
801 105
801 501
801 106
801 121
*/
DROP FUNCTION F_GETROOT
DROP TABLE BOM
GO
--生成测试数据
create table BOM_1(Item int,bom_head varchar(20),bom_child varchar(20),number int,products_attribute varchar(20))
insert into BOM_1 select 1 ,'A' ,'A1',1,'采购'
insert into BOM_1 select 2 ,'A' ,'A2',2,'生产'
insert into BOM_1 select 3 ,'A2','A3',3,'生产'
insert into BOM_1 select 4 ,'A2','A4',2,'采购'
insert into BOM_1 select 5 ,'A3','A5',2,'采购'
insert into BOM_1 select 6 ,'A3','A6',1,'采购'
insert into BOM_1 select 7 ,'B' ,'B1',1,'采购'
insert into BOM_1 select 8 ,'B' ,'B2',2,'生产'
insert into BOM_1 select 9 ,'B2','B3',3,'生产'
insert into BOM_1 select 10,'B2','B4',2,'采购'
insert into BOM_1 select 11,'B3','B5',2,'采购'
insert into BOM_1 select 12,'B3','B6',2,'采购'
go
--创建用户定义函数,用于取每个父节点下子节点的采购配置信息
create function f_stock(@bom_head varchar(20))
returns @t table(bom varchar(20),number int)
as
begin
declare @level int
declare @a table(bom varchar(20),number int,products_attribute varchar(20),[level] int)
set @level=1 if exists(select 1 from BOM_1 where bom_head=@bom_head)
insert into @a
select bom_child,number,products_attribute,@level
from BOM_1
where bom_head=@bom_head
while exists(select 1 from @a where [level]=@level and products_attribute='生产')
begin
set @level=@level+1
insert into @a(bom,number,products_attribute,[level])
select a.bom_child,a.number,a.products_attribute,@level
from BOM_1 a,@a b
where a.bom_head=b.bom and b.[level]=@level-1
end
insert into @t(bom,number) select bom,number from @a where products_attribute='采购'
return
end
go
--执行调用,取父节点'A'一个标准配置分解的采购信息及数量
select * from dbo.f_stock('A')
--生成测试数据
create table BOM(ID INT,PID INT,MSG VARCHAR(1000))
insert into BOM select 1,0,NULL
insert into BOM select 2,1,NULL
insert into BOM select 3,1,NULL
insert into BOM select 4,2,NULL
insert into BOM select 5,3,NULL
insert into BOM select 6,5,NULL
insert into BOM select 7,6,NULL
go--创建用户定义函数用于取每个父节点下子节点的采购配置信息
create function f_getChild(@ID VARCHAR(10))
returns @t table(ID VARCHAR(10),PID VARCHAR(10),Level INT)
as
begin
declare @i int
set @i = 1
insert into @t select ID,PID,@i from BOM where PID = @ID
while @@rowcount<>0
begin
set @i = @i + 1
insert into @t
select
a.ID,a.PID,@i
from
BOM a,@t b
where
a.PID=b.ID and b.Level = @i-1
end
return
end
go--执行查询
select ID from dbo.f_getChild(3)
go--输出结果
/*
ID
----
5
6
7
*/--删除测试数据
drop function f_getChild
drop table BOM创建用户定义函数,每个子节点de父节点的信息
--生成测试数据
create table BOM(ID int,parentID int,sClassName varchar(10))
insert into BOM values(1,0,'1111' )
insert into BOM values(2,1,'1111_1' )
insert into BOM values(3,2,'1111-1-1' )
insert into BOM values(4,3,'1111-1-1-1')
insert into BOM values(5,1,'1111-2' )go--创建用户定义函数,每个子节点de父节点的信息
create function f_getParent(@ID int)
returns varchar(40)
as
begin
declare @ret varchar(40) while exists(select 1 from BOM where ID=@ID and parentID<>0)
begin
select @ID=b.ID,@ret=','+rtrim(b.ID)+isnull(@ret,'')
from
BOM a,BOM b
where
a.ID=@ID and b.ID=a.parentID
end
set @ret=stuff(@ret,1,1,'')
return @ret
end
go--执行查询
select ID,isnull(dbo.f_getParent(ID),'') as parentID from BOM
go--输出结果
/*
ID parentID
----------- ----------------------------------------
1
2 1
3 1,2
4 1,2,3
5 1
*/--删除测试数据
drop function f_getParent
drop table BOM
go
Returns @TestTable Table(bono varchar(20),elno Varchar(20),boqty decimal(24,4),level int,t int)
As
Begin
declare @level int
set @level=1
---原料、成品、半成品全部写入临时表
Insert into @TestTable
select bono,elno,round(boqty/bounit*(1+elloss),4) as boqty,@level,0
from bomtable nolock
where bono =@bono
----成品半成品设置t值为1,只有是1的才进行折分
update @TestTable set t=1 where elno in (select bono from bomtable nolock) and level=@level
while @@rowcount>0
begin
set @level=@level+1
Insert into @TestTable
select a.bono,a.elno,round(a.boqty/a.bounit*(1+a.elloss),4) as boqty,@level,0
from bomtable a(nolock) ,@TestTable b
where a.bono =b.elno and b.level=@level-1 and b.t=1
----将半成品的 t 设为1 加以区分,只有是1的才进行折分
update @TestTable set t=1 where elno in (select bono from bomtable nolock) and level=@level
end
return
end
GO
至此,根据需要把指定料号赋值给此函数即可进行拆分。
查看sql server 2005教程