引用: --测试数据 深度排序 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) goselect * from areadrop 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 iddrop function f_cid drop proc my_proc drop table area , tmp/* name -------------- 广东省 东莞市 广州市 天河区 四川省 绵阳市 湖北省 武汉市 汉口区 随州市(所影响的行数为 10 行)*/本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/htl258/archive/2009/04/03/4014748.aspx
BOM?? 能不能通过查询子节点然后放入DATESET里面? --测试数据 深度排序 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 /*--结果 |--山东省 |--烟台市 |--招远市 |--青岛市 |--四会市 |--清远市 |--小分市 --*/
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
bom结构,直接放到表里,如楼上2位的经典代码
--树型结构处理之双编号(广度深度排序) if OBJECTPROPERTY(object_id('tb'),'isusertable')<>0 drop table tb create table tb(ybh nvarchar(10),ebh nvarchar(10),beizhu nvarchar(1000)) insert tb select '0001',null,'云南省' union all select '0002','0001','昆明市' union all select '0003','0001','昭通市' union all select '0009','0001','大理市' union all select '0008',null,'四川省' union all select '0004',null,'贵州省' union all select '0005','0002','五华区' union all select '0007','0002','水富县' union all select '0006','0005','西园路192号' union all select '0010','0006','金色梧桐3-702' union all select '0011','0010','科技有限公司' union all select '0015','0007','两碗乡' union all select '0013','0015','两碗村' union all select '0012','0013','某跨国集团董事长' union all select '0014','0008','成都市'--select * from tb --广度排序(先显示第一层节点,再显示第二次节点......) --定义辅助表 declare @level_tb table(bh nvarchar(10),level int) declare @level int set @level=0 insert @level_tb(bh,level) select ybh,@level from tb where ebh is null while @@ROWCOUNT>0 begin set @level=@level+1 insert @level_tb(bh,level) select ybh,@level from tb a,@level_tb b where a.ebh=b.bh and b.level=@level-1 end select a.*,b.* from tb a,@level_tb b where a.ybh=b.bh order by level /* ybh ebh beizhu bh level 0001 NULL 云南省 0001 0 0008 NULL 四川省 0008 0 0004 NULL 贵州省 0004 0 0002 0001 昆明市 0002 1 0003 0001 昭通市 0003 1 0009 0001 大理市 0009 1 0014 0008 成都市 0014 1 0005 0002 五华区 0005 2 0007 0002 水富县 0007 2 0006 0005 西园路192号 0006 3 0015 0007 两碗乡 0015 3 0010 0006 金色梧桐3-702 0010 4 0013 0015 两碗村 0013 4 0011 0010 科技有限公司 0011 5 0012 0013 某跨国集团董事长 0012 5 */ --深度排序(模拟单编码法) declare @level_tt table(ybh nvarchar(1000),ebh nvarchar(1000),level int) declare @level int set @level=0 insert @level_tt(ybh,ebh,level) select ybh,ybh,@level from tb where ebh is null while @@ROWCOUNT>0 begin set @level=@level+1 insert @level_tt(ybh,ebh,level) select a.ybh,b.ebh+a.ybh,@level from tb a,@level_tt b where a.ebh=b.ybh and b.level=@level-1 end select space(b.level*2)+'----'+a.beizhu,a.*,b.* from tb a,@level_tt b where a.ybh=b.ybh order by b.ebh /* (无列名) ybh ebh beizhu ybh ebh level ----云南省 0001 NULL 云南省 0001 0001 0 ----昆明市 0002 0001 昆明市 0002 00010002 1 ----五华区 0005 0002 五华区 0005 000100020005 2 ----西园路192号 0006 0005 西园路192号 0006 0001000200050006 3 ----金色梧桐3-702 0010 0006 金色梧桐3-702 0010 00010002000500060010 4 ----科技有限公司 0011 0010 科技有限公司 0011 000100020005000600100011 5 ----水富县 0007 0002 水富县 0007 000100020007 2 ----两碗乡 0015 0007 两碗乡 0015 0001000200070015 3 ----两碗村 0013 0015 两碗村 0013 00010002000700150013 4 ----某跨国集团董事长 0012 0013 某跨国集团董事长 0012 000100020007001500130012 5 ----昭通市 0003 0001 昭通市 0003 00010003 1 ----大理市 0009 0001 大理市 0009 00010009 1 ----贵州省 0004 NULL 贵州省 0004 0004 0 ----四川省 0008 NULL 四川省 0008 0008 0 ----成都市 0014 0008 成都市 0014 00080014 1 */不明白再说
--树型结构处理之双编号(广度深度排序) if OBJECTPROPERTY(object_id('tb'),'isusertable')<>0 drop table tb create table tb(ybh nvarchar(10),ebh nvarchar(10),beizhu nvarchar(1000)) insert tb select '0001',null,'中国' union all select '0002','0001','山东' union all select '0003','0002','潍坊' union all select '0008',null,'美国' union all select '0014','0008','亚利桑那州' union all select '0015','0014','某某市'-
--深度排序(模拟单编码法) declare @level_tt table(ybh nvarchar(1000),ebh nvarchar(1000),level int) declare @level int set @level=0 insert @level_tt(ybh,ebh,level) select ybh,ybh,@level from tb where ebh is null while @@ROWCOUNT>0 begin set @level=@level+1 insert @level_tt(ybh,ebh,level) select a.ybh,b.ebh+a.ybh,@level from tb a,@level_tt b where a.ebh=b.ybh and b.level=@level-1 end select space(b.level*2)+'----'+a.beizhu,a.*,b.* from tb a,@level_tt b where a.ybh=b.ybh order by b.ebh /* (无列名) ybh ebh beizhu ybh ebh level ----中国 0001 NULL 中国 0001 0001 0 ----山东 0002 0001 山东 0002 00010002 1 ----潍坊 0003 0002 潍坊 0003 000100020003 2 ----美国 0008 NULL 美国 0008 0008 0 ----亚利桑那州 0014 0008 亚利桑那州 0014 00080014 1 ----某某市 0015 0014 某某市 0015 000800140015 2 */类似数据修改一下即可!
楼主是不是您有三个表,分别记录了这些层次结构的数据呀。我也遇到过这个情况,不过实现的方法不是很科学,我的表是有三层结构,支店,地区,店铺这三层,分别对应表Footholds,area,branches,因为这样的数据需要绑定到下拉框里,所以,还生成了一个字段用来记录个个层次的值,代码如下: SELECT CASE WHEN [BLOCKCD] <> 0 AND [AREACD] <> 0 THEN '--' + AREA ELSE CASE WHEN [BLOCKCD] <> 0 THEN '-' + BLOCK ELSE BRANCH END END AS [LIST] ,ORD FROM ( SELECT FootholdCD AS [BRANCHCD] ,FootholdName AS BRANCH ,0 AS [BLOCKCD] ,'' AS BLOCK ,0 AS [AREACD] ,'' AS AREA ,CAST(FootholdCD AS VARCHAR(4)) + '-0-0' AS ORD FROM MasterUpdate.dbo.Footholds UNION
SELECT F.FootholdCD AS [BRANCHCD] ,F.FootholdName AS BRANCH ,BlockCD AS [BLOCKCD] ,BlockName AS BLOCK ,0 AS [AREACD] ,'' AS AREA ,CAST(B.FootholdCD AS VARCHAR(4)) + '-' + CAST(BlockCD AS VARCHAR(4)) + '-0' AS ORD FROM MasterUpdate.dbo.Blocks B JOIN MasterUpdate.dbo.Footholds F ON B.FootholdCD = F.FootholdCD
UNION
SELECT F.FootholdCD AS [BRANCHCD] ,F.FootholdName AS BRANCH ,B.BlockCD AS [BLOCKCD] ,B.BlockName AS BLOCK ,AreaCD AS [AREACD] ,AreaName AS AREA ,CAST(F.FootholdCD AS VARCHAR(4)) + '-' + CAST(A.BlockCD AS VARCHAR(4)) + '-' + CAST(A.AreaCD AS VARCHAR(4)) AS ORD FROM MasterUpdate.dbo.Areas A JOIN MasterUpdate.dbo.Blocks B ON A.BlockCD = B.BlockCD JOIN MasterUpdate.dbo.Footholds F ON B.FootholdCD = F.FootholdCD ) A ORDER BY A.BRANCHCD ,A.BLOCKCD ,A.AREACD 此方法仅供楼主参考。祝你开心。
--测试数据 深度排序
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)
goselect * from areadrop 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 iddrop function f_cid
drop proc my_proc
drop table area , tmp/*
name
--------------
广东省
东莞市
广州市
天河区
四川省
绵阳市
湖北省
武汉市
汉口区
随州市(所影响的行数为 10 行)*/本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/htl258/archive/2009/04/03/4014748.aspx
能不能通过查询子节点然后放入DATESET里面?
--测试数据 深度排序
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
/*--结果
|--山东省
|--烟台市
|--招远市
|--青岛市
|--四会市
|--清远市
|--小分市
--*/
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
if OBJECTPROPERTY(object_id('tb'),'isusertable')<>0
drop table tb
create table tb(ybh nvarchar(10),ebh nvarchar(10),beizhu nvarchar(1000))
insert tb
select '0001',null,'云南省'
union all select '0002','0001','昆明市'
union all select '0003','0001','昭通市'
union all select '0009','0001','大理市'
union all select '0008',null,'四川省'
union all select '0004',null,'贵州省'
union all select '0005','0002','五华区'
union all select '0007','0002','水富县'
union all select '0006','0005','西园路192号'
union all select '0010','0006','金色梧桐3-702'
union all select '0011','0010','科技有限公司'
union all select '0015','0007','两碗乡'
union all select '0013','0015','两碗村'
union all select '0012','0013','某跨国集团董事长'
union all select '0014','0008','成都市'--select * from tb
--广度排序(先显示第一层节点,再显示第二次节点......)
--定义辅助表
declare @level_tb table(bh nvarchar(10),level int)
declare @level int
set @level=0
insert @level_tb(bh,level)
select ybh,@level from tb where ebh is null
while @@ROWCOUNT>0
begin
set @level=@level+1
insert @level_tb(bh,level)
select ybh,@level
from tb a,@level_tb b
where a.ebh=b.bh
and b.level=@level-1
end
select a.*,b.* from tb a,@level_tb b where a.ybh=b.bh order by level
/*
ybh ebh beizhu bh level
0001 NULL 云南省 0001 0
0008 NULL 四川省 0008 0
0004 NULL 贵州省 0004 0
0002 0001 昆明市 0002 1
0003 0001 昭通市 0003 1
0009 0001 大理市 0009 1
0014 0008 成都市 0014 1
0005 0002 五华区 0005 2
0007 0002 水富县 0007 2
0006 0005 西园路192号 0006 3
0015 0007 两碗乡 0015 3
0010 0006 金色梧桐3-702 0010 4
0013 0015 两碗村 0013 4
0011 0010 科技有限公司 0011 5
0012 0013 某跨国集团董事长 0012 5
*/
--深度排序(模拟单编码法)
declare @level_tt table(ybh nvarchar(1000),ebh nvarchar(1000),level int)
declare @level int
set @level=0
insert @level_tt(ybh,ebh,level)
select ybh,ybh,@level from tb where ebh is null
while @@ROWCOUNT>0
begin
set @level=@level+1
insert @level_tt(ybh,ebh,level)
select a.ybh,b.ebh+a.ybh,@level
from tb a,@level_tt b
where a.ebh=b.ybh and b.level=@level-1
end
select space(b.level*2)+'----'+a.beizhu,a.*,b.*
from tb a,@level_tt b
where a.ybh=b.ybh
order by b.ebh
/*
(无列名) ybh ebh beizhu ybh ebh level
----云南省 0001 NULL 云南省 0001 0001 0
----昆明市 0002 0001 昆明市 0002 00010002 1
----五华区 0005 0002 五华区 0005 000100020005 2
----西园路192号 0006 0005 西园路192号 0006 0001000200050006 3
----金色梧桐3-702 0010 0006 金色梧桐3-702 0010 00010002000500060010 4
----科技有限公司 0011 0010 科技有限公司 0011 000100020005000600100011 5
----水富县 0007 0002 水富县 0007 000100020007 2
----两碗乡 0015 0007 两碗乡 0015 0001000200070015 3
----两碗村 0013 0015 两碗村 0013 00010002000700150013 4
----某跨国集团董事长 0012 0013 某跨国集团董事长 0012 000100020007001500130012 5
----昭通市 0003 0001 昭通市 0003 00010003 1
----大理市 0009 0001 大理市 0009 00010009 1
----贵州省 0004 NULL 贵州省 0004 0004 0
----四川省 0008 NULL 四川省 0008 0008 0
----成都市 0014 0008 成都市 0014 00080014 1
*/不明白再说
if OBJECTPROPERTY(object_id('tb'),'isusertable')<>0
drop table tb
create table tb(ybh nvarchar(10),ebh nvarchar(10),beizhu nvarchar(1000))
insert tb
select '0001',null,'中国'
union all select '0002','0001','山东'
union all select '0003','0002','潍坊'
union all select '0008',null,'美国'
union all select '0014','0008','亚利桑那州'
union all select '0015','0014','某某市'-
--深度排序(模拟单编码法)
declare @level_tt table(ybh nvarchar(1000),ebh nvarchar(1000),level int)
declare @level int
set @level=0
insert @level_tt(ybh,ebh,level)
select ybh,ybh,@level from tb where ebh is null
while @@ROWCOUNT>0
begin
set @level=@level+1
insert @level_tt(ybh,ebh,level)
select a.ybh,b.ebh+a.ybh,@level
from tb a,@level_tt b
where a.ebh=b.ybh and b.level=@level-1
end
select space(b.level*2)+'----'+a.beizhu,a.*,b.*
from tb a,@level_tt b
where a.ybh=b.ybh
order by b.ebh
/*
(无列名) ybh ebh beizhu ybh ebh level
----中国 0001 NULL 中国 0001 0001 0
----山东 0002 0001 山东 0002 00010002 1
----潍坊 0003 0002 潍坊 0003 000100020003 2
----美国 0008 NULL 美国 0008 0008 0
----亚利桑那州 0014 0008 亚利桑那州 0014 00080014 1
----某某市 0015 0014 某某市 0015 000800140015 2
*/类似数据修改一下即可!
SELECT
CASE WHEN [BLOCKCD] <> 0 AND [AREACD] <> 0 THEN '--' + AREA ELSE CASE WHEN [BLOCKCD] <> 0 THEN '-' + BLOCK ELSE BRANCH END END AS [LIST]
,ORD
FROM (
SELECT
FootholdCD AS [BRANCHCD]
,FootholdName AS BRANCH
,0 AS [BLOCKCD]
,'' AS BLOCK
,0 AS [AREACD]
,'' AS AREA
,CAST(FootholdCD AS VARCHAR(4)) + '-0-0' AS ORD
FROM
MasterUpdate.dbo.Footholds
UNION
SELECT
F.FootholdCD AS [BRANCHCD]
,F.FootholdName AS BRANCH
,BlockCD AS [BLOCKCD]
,BlockName AS BLOCK
,0 AS [AREACD]
,'' AS AREA
,CAST(B.FootholdCD AS VARCHAR(4)) + '-' + CAST(BlockCD AS VARCHAR(4)) + '-0' AS ORD
FROM
MasterUpdate.dbo.Blocks B
JOIN
MasterUpdate.dbo.Footholds F
ON
B.FootholdCD = F.FootholdCD
UNION
SELECT
F.FootholdCD AS [BRANCHCD]
,F.FootholdName AS BRANCH
,B.BlockCD AS [BLOCKCD]
,B.BlockName AS BLOCK
,AreaCD AS [AREACD]
,AreaName AS AREA
,CAST(F.FootholdCD AS VARCHAR(4)) + '-' + CAST(A.BlockCD AS VARCHAR(4)) + '-' + CAST(A.AreaCD AS VARCHAR(4)) AS ORD
FROM
MasterUpdate.dbo.Areas A
JOIN
MasterUpdate.dbo.Blocks B
ON
A.BlockCD = B.BlockCD
JOIN
MasterUpdate.dbo.Footholds F
ON
B.FootholdCD = F.FootholdCD
) A
ORDER BY
A.BRANCHCD
,A.BLOCKCD
,A.AREACD
此方法仅供楼主参考。祝你开心。
呵呵,楼主就直接把这3个级别的代码都放一个字段,或2个字段里,然后用楼上的经典bom结构代码就可以搞定
但是不是一般的容易我是看了就晕,一直没敢去看这东东