日期:2014-05-17 浏览次数:20538 次
--测试数据 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 /*--结果 |--山东省 |--烟台市 |--招远市 |--青岛市 |--四会市 |--清远市 |--小分市 --*/ --2005的方法 declare @T table (ID int,pid int,NAME varchar(6)) insert into @T select 1,0,'上衣' union all select 2,0,'鞋子' union all select 3,0,'裤子' union all select 4,1,'毛衣' union all select 5,1,'衬衫' union all select 6,2,'球鞋' union all select 7,2,'皮鞋' union all select 8,3,'西裤' union all select 9,3,'筒裤' union all select 10,4,'羊毛衣' union all select 11,4,'牛毛衣' union all select 12,5,'白衬衫' union all select 13,5,'黑衬衫' ;with depts as( select * from @T where ID = 1 union all select a.* from @T a, depts b where a.pid = b.ID ) select * from depts /* ID pid NAME ----------- ----------- ------ 1 0 上衣 4 1 毛衣 5 1 衬衫 12 5 白衬衫 13 5 黑衬衫 10 4 羊毛衣 11 4 牛毛衣 */ ------------------------------------------------------ CREATE TABLE [dbo].[levelTable]( [ID] [int] IDENTITY(1,1) NOT NULL, [parentID] [int] NOT NULL CONSTRAINT [DF_levelTable_parentID] DEFAULT ((0)), [name] [nvarchar](50) NULL ) insert into levelTable(parentID,[NAME]) values(0,'开发部') insert into levelTable(parentID,[NAME]) values(0,'客户服务部') insert into levelTable(parentID,[NAME]) values(0,'行政部') insert into levelTable(parentID,[NAME]) values(1,'开发一部') insert into levelTable(parentID,[NAME]) values(1,'开发二部') insert into levelTable(parentID,[NAME]) values(2,'后勤服务部') insert into levelTable(parentID,[NAME]) values(2,'大厅服务部') insert into levelTable(parentID,[NAME]) values(3,'总裁部') insert into levelTable(parentID,[NAME]) values(3,'人力资源部') insert into levelTable(parentID,[NAME]) values(9,'员工管理部') insert into levelTable(parentID,[NAME]) values(9,'人员招聘部') insert into levelTable(parentID,[NAME]) values(5,'开发一部项目A部') go DECLARE @t_Level TABLE(ID char(3),Level int,Sort varchar(100)) DECLARE @Level int SET @Level=0 INSERT @t_Level SELECT id,@Level,right('1000'+ltrim(ID),3) FROM levelTable WHERE parentID =0 -------modify WHILE @@ROWCOUNT>0 BEGIN SET @Level=@Level+1 INSERT @t_Level SELECT a.ID,@Level,ltrim(b.Sort)+right('1000'+ltrim(a.ID),3) FROM levelTable a,@t_Level b WHERE a.parentID=b.ID AND b.Level=@Level-1 END --显示结果 SELECT a