现在的位置: 首页 > 综合 > 正文

BOM展开算法

2014年03月07日 ⁄ 综合 ⁄ 共 6490字 ⁄ 字号 评论关闭

引用:  
  --测试数据   深度排序    
  DECLARE   @t   TABLE(ID   char(3),PID   char(3),Name   nvarchar(10))  
  INSERT   @t   SELECT   '001',NULL   ,'山东省'  
  UNION   ALL   SELECT   '002','001','烟台市'  
  UNION   ALL   SELECT   '004','002','招远市'  
  UNION   ALL   SELECT   '003','001','青岛市'  
  UNION   ALL   SELECT   '005',NULL   ,'四会市'  
  UNION   ALL   SELECT   '006','005','清远市'  
  UNION   ALL   SELECT   '007','006','小分市'  
   
  --深度排序显示处理  
  --生成每个节点的编码累计(相同当单编号法的编码)  
  DECLARE   @t_Level   TABLE(ID   char(3),Level   int,Sort   varchar(8000))  
  DECLARE   @Level   int  
  SET   @Level=0  
  INSERT   @t_Level   SELECT   ID,@Level,ID  
  FROM   @t  
  WHERE   PID   IS   NULL  
  WHILE   @@ROWCOUNT>0  
  BEGIN  
  SET   @Level=@Level+1  
  INSERT   @t_Level   SELECT   a.ID,@Level,b.Sort+a.ID  
  FROM   @t   a,@t_Level   b  
  WHERE   a.PID=b.ID  
  AND   b.Level=@Level-1  
  END  
   
  --显示结果  
  SELECT   a.*  
  FROM   @t   a,@t_Level   b  
  WHERE   a.ID=b.ID  
  ORDER   BY   b.Sort  
  /*--结果  
  ID       PID       Name                
  ------   ---------   ----------    
  001     NULL   山东省  
  002     001       烟台市  
  004     002       招远市  
  003     001       青岛市  
  005     NULL   四会市  
  006     005       清远市  
  007     006       小分市  
  --*/

--查询指定节点及其所有子节点的函数  
  CREATE   FUNCTION   f_Cid(@ID   char(3))  
  RETURNS   @t_Level   TABLE(ID   char(3),Level   int)  
  AS  
  BEGIN  
  DECLARE   @Level   int  
  SET   @Level=1  
  INSERT   @t_Level   SELECT   @ID,@Level  
  WHILE   @@ROWCOUNT>0  
  BEGIN  
  SET   @Level=@Level+1  
  INSERT   @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  
   
  --调用函数查询002及其所有子节点  
  SELECT   a.*  
  FROM   tb   a,f_Cid('002')   b  
  WHERE   a.ID=b.ID  
  /*--结果  
  ID       PID     Name                
  ------   -------   ----------    
  002     001     烟台市  
  004     002     招远市  
  --*/

--测试数据
DECLARE @t TABLE(ID char(3),PID char(3),Name nvarchar(10))
INSERT @t SELECT '001',NULL ,'山东省'
UNION ALL SELECT '002','001','烟台市'
UNION ALL SELECT '004','002','招远市'
UNION ALL SELECT '003','001','青岛市'
UNION ALL SELECT '005',NULL ,'四会市'
UNION ALL SELECT '006','005','清远市'
UNION ALL SELECT '007','006','小分市'

--深度排序显示处理
--生成每个节点的编码累计(相同当单编号法的编码)
DECLARE @t_Level TABLE(ID char(3),Level int,Sort varchar(8000))
DECLARE @Level int
SET @Level=0
INSERT @t_Level SELECT ID,@Level,ID
FROM @t
WHERE PID IS NULL
WHILE @@ROWCOUNT>0
BEGIN
    SET @Level=@Level+1
    INSERT @t_Level SELECT a.ID,@Level,b.Sort+a.ID
    FROM @t a,@t_Level b
    WHERE a.PID=b.ID
        AND b.Level=@Level-1
END

--显示结果
SELECT SPACE(b.Level*2)+'|--'+a.Name
FROM @t a,@t_Level b
WHERE a.ID=b.ID
ORDER BY b.Sort
/*--结果
|--山东省
  |--烟台市
    |--招远市
  |--青岛市
|--四会市
  |--清远市
    |--小分市
--*/

/*
标题:查询指定节点及其所有子节点的函数
作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开)
时间: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

 

多个的使用临时表组合数据,参考:

 

SQL code

/*
标题:查询所有顶级节点及其子节点的例
地址:http://topic.csdn.net/u/20090323/21/63a91f51-c4df-464d-ba18-64343deb4e3a.html
作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开)
时间:2009-03-23
地点:广东深圳
*/

[code=SQL]create table Area (id int identity,Name varchar(10) ,order_by int ,father_ID int )
insert into area values('广东省',2,0)
insert into area values('四川省',2,0)
insert into area values('湖北省',2,0)
insert into area values('东莞市',1,1)
insert into area values('广州市',1,1)
insert into area values('天河区',0,5)
insert into area values('绵阳市',1,2)
insert into area values('武汉市',1,3)
insert into area values('汉口区',0,8)
insert into area values('随州市',1,3)
go

select * from area

drop table area

/*
id          Name       order_by    father_ID  
----------- ---------- ----------- -----------
1           广东省        2           0
2           四川省        2           0
3           湖北省        2           0
4           东莞市        1           1
5           广州市        1           1
6           天河区        0           5
7           绵阳市        1           2
8           武汉市        1           3
9           汉口区        0           8
10          随州市        1           3

(所影响的行数为 10 行)

要求显示为:
name          
--------------
广东省
  东莞市
  广州市
    天河区
四川省
  绵阳市
湖北省
  武汉市
    汉口区
  随州市

(所影响的行数为 10 行)
*/

 

SQL code

--创建原始表
create table Area (id int identity,Name varchar(10) ,order_by int ,father_ID int )
insert into area values('广东省',2,0)
insert into area values('四川省',2,0)
insert into area values('湖北省',2,0)
insert into area values('东莞市',1,1)
insert into area values('广州市',1,1)
insert into area values('天河区',0,5)
insert into area values('绵阳市',1,2)
insert into area values('武汉市',1,3)
insert into area values('汉口区',0,8)
insert into area values('随州市',1,3)
--创建临时表
create table tmp (id int identity,Name varchar(10) ,order_by int ,father_ID int )
go

--创建查询指定节点及其所有子节点的函数
create function f_cid(@ID int) returns @t_level table(id int , 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 area a , @t_Level b
    where a.father_ID = b.id and b.level = @level - 1
  end
  return
end
go

--创建存储过程并将数据插入临时表
create proc my_proc
as
begin
  declare @id as int
  set @id = 0
  while exists(select 1 from area where order_by = 2 and id > @id)
  begin
    set @id = (select min(id) from area where order_by = 2 and id > @id)
    insert into tmp(Name ,order_by ,father_ID) select a.name,a.order_by ,a.father_id from area a , f_cid(@id) b where a.id = b.id order by a.id
  end
end
go
exec my_proc

--从临时表提取数据并显示
select case when order_by = 2 then name
            when order_by = 1 then '  ' + name
            when order_by = 0 then '    ' + name
       end name
from tmp order by id

drop function f_cid
drop proc my_proc
drop table area , tmp

/*
name          
--------------
广东省
  东莞市
  广州市
    天河区
四川省
  绵阳市
湖北省
  武汉市
    汉口区
  随州市

(所影响的行数为 10 行)

*/

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/htl258/archive/2009/03/22/4014748.aspx

抱歉!评论已关闭.