码迷,mamicode.com
首页 > 数据库 > 详细

Sql Server按树形结构排序查询表记录

时间:2015-10-13 15:06:46      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:

http://blog.csdn.net/dxnn520/article/details/8089149
--------------------------------------------------------------------------

--  Author : htl258(Tony)

--  Date   : 2010-04-23 02:37:28

--  Version:Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86) 

--          Jul  9 2008 14:43:34 

--          Copyright (c) 1988-2008 Microsoft Corporation

--          Developer Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 3)

--  Subject: BOM按节点排序应用实例

--------------------------------------------------------------------------

 

--实例1:

--> 生成测试数据表:tb

 

IF NOT OBJECT_ID([tb]) IS NULL

    DROP TABLE [tb]

GO

CREATE TABLE [tb]([id] INT,[code] NVARCHAR(10),[pid] INT,[name] NVARCHAR(10))

INSERT [tb]

SELECT 1,01,0,N服装 UNION ALL

SELECT 2,01,1,N男装 UNION ALL

SELECT 3,01,2,N西装 UNION ALL

SELECT 4,01,3,N全毛 UNION ALL

SELECT 5,02,3,N化纤 UNION ALL

SELECT 6,02,2,N休闲装 UNION ALL

SELECT 7,02,1,N女装 UNION ALL

SELECT 8,01,7,N套装 UNION ALL

SELECT 9,02,7,N职业装 UNION ALL

SELECT 10,03,7,N休闲装 UNION ALL

SELECT 11,04,7,N西装 UNION ALL

SELECT 12,01,11,N全毛 UNION ALL

SELECT 13,02,11,N化纤 UNION ALL

SELECT 14,05,7,N休闲装

GO

--SELECT * FROM [tb]

 

-->SQL查询如下:

 

;WITH T AS

(

    SELECT CAST(CODE AS VARCHAR(20)) AS CODE,*,

        CAST(ID AS VARBINARY(MAX)) AS px 

    FROM tb AS A

    WHERE NOT EXISTS(SELECT * FROM tb WHERE id=A.pid)

    UNION ALL 

    SELECT CAST(B.CODE+A.CODE AS VARCHAR(20)),A.*,

         CAST(B.px+CAST(A.ID AS VARBINARY) AS VARBINARY(MAX))    

    FROM tb AS A

        JOIN T AS B

           ON A.pid=B.id

)

SELECT Code,Name FROM T 

ORDER BY px

/*

Code                 Name

-------------------- ----------

01                   服装

0101                 男装

010101               西装

01010101             全毛

01010102             化纤

010102               休闲装

0102                 女装

010201               套装

010202               职业装

010203               休闲装

010204               西装

01020401             全毛

01020402             化纤

010205               休闲装

 

(14 行受影响)

*/

 

--实例2:

--> 生成测试数据表:tb

IF NOT OBJECT_ID([tb]) IS NULL
    DROP TABLE [tb]
GO
CREATE TABLE [tb]([id] INT,[parentid] INT,[categoryname] NVARCHAR(10))
INSERT [tb]
SELECT 1,0,test1 UNION ALL
SELECT 2,0,test2 UNION ALL
SELECT 3,1,test1.1 UNION ALL
SELECT 4,2,test2.1 UNION ALL
SELECT 5,3,test1.1.1 UNION ALL
SELECT 6,1,test1.2
GO
--SELECT * FROM [tb]

-->SQL查询如下:
;WITH T AS
(
    SELECT *,CAST(ID AS VARBINARY(MAX)) AS px 
    FROM tb AS A
    WHERE NOT EXISTS(SELECT * FROM tb WHERE id=A.[parentid])
    UNION ALL 
    SELECT A.*,CAST(B.px+CAST(A.ID AS VARBINARY) AS VARBINARY(MAX))  
    FROM tb AS A
        JOIN T AS B
           ON A.[parentid]=B.id
)
SELECT [id],[parentid],[categoryname] FROM T 
ORDER BY px
/*
id          parentid    categoryname
----------- ----------- ------------
1           0           test1
3           1           test1.1
5           3           test1.1.1
6           1           test1.2
2           0           test2
4           2           test2.1

(6 行受影响)
*/



本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/htl258/archive/2010/04/23/5518166.aspx

 复杂例:

技术分享
USE [YZL]
GO
/****** Object:  Table [dbo].[P_DictionaryType]    Script Date: 10/13/2015 14:59:57 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[P_DictionaryType](
    [DT_ObjectID] [uniqueidentifier] NOT NULL,
    [DT_Name] [nvarchar](100) NULL,
    [DT_ShortName] [nvarchar](100) NULL,
    [DT_Number] [nvarchar](100) NULL,
    [DT_Introduction] [text] NULL,
    [DT_IsDelete] [bit] NULL,
 CONSTRAINT [PK_P_DICTIONARYTYPE] PRIMARY KEY CLUSTERED 
(
    [DT_ObjectID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
INSERT [dbo].[P_DictionaryType] ([DT_ObjectID], [DT_Name], [DT_ShortName], [DT_Number], [DT_Introduction], [DT_IsDelete]) VALUES (N6937f4c4-0656-4ce7-8049-078b7199c26d, N行政职务, NXZZW, NDT_XZZW, N‘‘, 0)
INSERT [dbo].[P_DictionaryType] ([DT_ObjectID], [DT_Name], [DT_ShortName], [DT_Number], [DT_Introduction], [DT_IsDelete]) VALUES (Ndce2ea08-1bd8-4b36-a39c-0e0ee1c253f4, N劳动合同, NDT_LDHT, NDT_LDHT, NULL, 0)
INSERT [dbo].[P_DictionaryType] ([DT_ObjectID], [DT_Name], [DT_ShortName], [DT_Number], [DT_Introduction], [DT_IsDelete]) VALUES (Nd5a92adc-886f-420b-ae78-0ef161e4aaf6, N国籍, NDT_GJ, NDT_GJ, NULL, 0)
INSERT [dbo].[P_DictionaryType] ([DT_ObjectID], [DT_Name], [DT_ShortName], [DT_Number], [DT_Introduction], [DT_IsDelete]) VALUES (N227f7bd8-bf36-48ce-9221-1155b218da6d, N计算机等级, NDT_JSJDJ, NDT_JSJDJ, NULL, 0)
INSERT [dbo].[P_DictionaryType] ([DT_ObjectID], [DT_Name], [DT_ShortName], [DT_Number], [DT_Introduction], [DT_IsDelete]) VALUES (Nb5ff1e1c-2802-49ad-a9f0-1331ec704207, N岗位类型, NGWLX, NDT_GWLB, N‘‘, 0)
INSERT [dbo].[P_DictionaryType] ([DT_ObjectID], [DT_Name], [DT_ShortName], [DT_Number], [DT_Introduction], [DT_IsDelete]) VALUES (Nbf309285-b83e-404a-8baa-167b88028db5, N专业技术职务, NZYJSZW, NDT_ZYJSZW, N‘‘, 0)
INSERT [dbo].[P_DictionaryType] ([DT_ObjectID], [DT_Name], [DT_ShortName], [DT_Number], [DT_Introduction], [DT_IsDelete]) VALUES (N5c58325f-a70b-4ab1-a0f0-1eb07aa1c14f, N课程类型, N课程, NDT_KCLX, N课程, 0)
INSERT [dbo].[P_DictionaryType] ([DT_ObjectID], [DT_Name], [DT_ShortName], [DT_Number], [DT_Introduction], [DT_IsDelete]) VALUES (Na5622506-2d39-4c31-99d3-216e4c19fc77, N健康状况, N‘‘, NDT_JKZK, N‘‘, 0)
INSERT [dbo].[P_DictionaryType] ([DT_ObjectID], [DT_Name], [DT_ShortName], [DT_Number], [DT_Introduction], [DT_IsDelete]) VALUES (N029c58d8-9ab3-4ef9-807c-275c3fa6dba8, N身份证件类型, NZJLX, NDT_SFZJLX, N身份证件类型, 0)
INSERT [dbo].[P_DictionaryType] ([DT_ObjectID], [DT_Name], [DT_ShortName], [DT_Number], [DT_Introduction], [DT_IsDelete]) VALUES (N0f3b1b82-daae-4e5a-a0db-2b565f34138a, N教师职称, NDT_JSZC, NDT_JSZC, NULL, 0)
INSERT [dbo].[P_DictionaryType] ([DT_ObjectID], [DT_Name], [DT_ShortName], [DT_Number], [DT_Introduction], [DT_IsDelete]) VALUES (Nf5c29858-238c-41fe-9bc4-2ca0acec613d, N民族, N民族, NDT_MZ, N民族, 0)
INSERT [dbo].[P_DictionaryType] ([DT_ObjectID], [DT_Name], [DT_ShortName], [DT_Number], [DT_Introduction], [DT_IsDelete]) VALUES (N2a800f43-13bb-4795-ab1b-5633f4eac80d, N学生类型, N‘‘, NDT_XSLX, N‘‘, 0)
INSERT [dbo].[P_DictionaryType] ([DT_ObjectID], [DT_Name], [DT_ShortName], [DT_Number], [DT_Introduction], [DT_IsDelete]) VALUES (Ne770aff5-beb5-4c41-8935-5bf1177ba405, N专任教师接受培训情况, NDT_ZRJSJSPXQK, NDT_ZRJSJSPXQK, N‘‘, 0)
INSERT [dbo].[P_DictionaryType] ([DT_ObjectID], [DT_Name], [DT_ShortName], [DT_Number], [DT_Introduction], [DT_IsDelete]) VALUES (Nf16ea79f-8210-4992-b5ae-60761959e9d3, N签订合同情况, NDT_QDHTQK, NDT_QDHTQK, NULL, 0)
INSERT [dbo].[P_DictionaryType] ([DT_ObjectID], [DT_Name], [DT_ShortName], [DT_Number], [DT_Introduction], [DT_IsDelete]) VALUES (N105343d1-3997-4f1b-ae32-66f713417707, N费用类型, NDT_FYLX, NDT_FYLX, NULL, 0)
INSERT [dbo].[P_DictionaryType] ([DT_ObjectID], [DT_Name], [DT_ShortName], [DT_Number], [DT_Introduction], [DT_IsDelete]) VALUES (N76c6e55e-331c-4e9a-af91-776801e1f0a6, N户口性质, NHKXZ, NDT_HKXZ, N‘‘, 0)
INSERT [dbo].[P_DictionaryType] ([DT_ObjectID], [DT_Name], [DT_ShortName], [DT_Number], [DT_Introduction], [DT_IsDelete]) VALUES (Nf674b9ac-0bfd-4fa9-b88d-816364616714, N荣誉类型, NRYLX, NDT_RYLX, NULL, 0)
INSERT [dbo].[P_DictionaryType] ([DT_ObjectID], [DT_Name], [DT_ShortName], [DT_Number], [DT_Introduction], [DT_IsDelete]) VALUES (Ncac52b7e-1f4f-4d09-9903-8360a7750c65, N企业工作(实践)时长, NQYGZ_SJ_SC, NDT_QYGZ_SJ_SC, N‘‘, 0)
INSERT [dbo].[P_DictionaryType] ([DT_ObjectID], [DT_Name], [DT_ShortName], [DT_Number], [DT_Introduction], [DT_IsDelete]) VALUES (N27927422-1e91-4ff2-aef0-8942fd30aef8, N普通话等级, NPTHDJ, NDT_PTHDJ, N‘‘, 0)
INSERT [dbo].[P_DictionaryType] ([DT_ObjectID], [DT_Name], [DT_ShortName], [DT_Number], [DT_Introduction], [DT_IsDelete]) VALUES (N12965e30-9b7a-4c38-af54-8c4b7303f1e6, N奖学金, N‘‘, NDT_JXJ, N为资助世界各国学生、学者到中国高等学校进行学习和研究,增进中国人民与世界各国人民的相互理解和友谊,发展中国与世界各国在教育、科技、文化、经贸等领域的交流与合作,中国政府设立了中国政府奖学金。教育部负责根据中国政府与外国政府或国际组织达成的协议或计划对外提供中国政府奖学金, 0)
INSERT [dbo].[P_DictionaryType] ([DT_ObjectID], [DT_Name], [DT_ShortName], [DT_Number], [DT_Introduction], [DT_IsDelete]) VALUES (N22965e30-9b7a-4c38-af54-8c4b7303f1e6, N五险一金, N, NDT_WXYJ, N你知道什么是五险一金吗?, 0)
INSERT [dbo].[P_DictionaryType] ([DT_ObjectID], [DT_Name], [DT_ShortName], [DT_Number], [DT_Introduction], [DT_IsDelete]) VALUES (N9c21ebf6-5a99-4e45-9b0d-8ce4f23de110, N助学金, NDT_ZXJ, NDT_ZXJ, NULL, 0)
INSERT [dbo].[P_DictionaryType] ([DT_ObjectID], [DT_Name], [DT_ShortName], [DT_Number], [DT_Introduction], [DT_IsDelete]) VALUES (N6d660ae9-2c48-4ad2-abee-8e481199b04b, N取得其他职业资格证书, NQDQTZYZGZS, NDT_QDQTZYZGZS, N‘‘, 0)
INSERT [dbo].[P_DictionaryType] ([DT_ObjectID], [DT_Name], [DT_ShortName], [DT_Number], [DT_Introduction], [DT_IsDelete]) VALUES (N0f65f497-f371-48e3-b0b1-9226487a1c35, N任教学科类别, NRJXKLB, NDT_RJXKLB, N‘‘, 0)
INSERT [dbo].[P_DictionaryType] ([DT_ObjectID], [DT_Name], [DT_ShortName], [DT_Number], [DT_Introduction], [DT_IsDelete]) VALUES (Ndd3afa0d-5c46-4820-a226-95ab8fa82134, N生源类型, N‘‘, NDT_SYLB, N‘‘, 0)
INSERT [dbo].[P_DictionaryType] ([DT_ObjectID], [DT_Name], [DT_ShortName], [DT_Number], [DT_Introduction], [DT_IsDelete]) VALUES (N114c7ea1-aef4-4aba-bc3e-9c8a737b2682, N请假类型, NDT_QJLX, NDT_QJLX, NULL, 0)
INSERT [dbo].[P_DictionaryType] ([DT_ObjectID], [DT_Name], [DT_ShortName], [DT_Number], [DT_Introduction], [DT_IsDelete]) VALUES (Nef31b612-f457-41a1-a4bc-9d416ec2a757, N任课状况, NRKZK, NDT_RKZK, N‘‘, 0)
INSERT [dbo].[P_DictionaryType] ([DT_ObjectID], [DT_Name], [DT_ShortName], [DT_Number], [DT_Introduction], [DT_IsDelete]) VALUES (N5e9d84b6-2cf2-4590-910e-9db30ee8126c, N政治面貌, N政治面貌, NDT_ZZMM, N‘‘, 0)
INSERT [dbo].[P_DictionaryType] ([DT_ObjectID], [DT_Name], [DT_ShortName], [DT_Number], [DT_Introduction], [DT_IsDelete]) VALUES (Nb9c3ec2d-6708-4020-b93f-9f664575f7eb, N获奖等级, NHJDJ, NDT_HJDJ, NULL, 0)
INSERT [dbo].[P_DictionaryType] ([DT_ObjectID], [DT_Name], [DT_ShortName], [DT_Number], [DT_Introduction], [DT_IsDelete]) VALUES (Nbcd1fbb8-9209-44d7-af77-9f6ecec246c0, N学历, NXL, NDT_XL, N‘‘, 0)
INSERT [dbo].[P_DictionaryType] ([DT_ObjectID], [DT_Name], [DT_ShortName], [DT_Number], [DT_Introduction], [DT_IsDelete]) VALUES (Nb39e45dd-7ac2-4ab2-8241-a3fdebd9f4eb, N教师资格证, NJSZGZ, NDT_JSZGZ, N‘‘, 0)
INSERT [dbo].[P_DictionaryType] ([DT_ObjectID], [DT_Name], [DT_ShortName], [DT_Number], [DT_Introduction], [DT_IsDelete]) VALUES (N4d802690-51ed-45ee-90a0-adee3e7e4bde, N婚姻状况, NDT_HYZK, NDT_HYZK, NULL, 0)
INSERT [dbo].[P_DictionaryType] ([DT_ObjectID], [DT_Name], [DT_ShortName], [DT_Number], [DT_Introduction], [DT_IsDelete]) VALUES (Nad4fcf0a-a68c-4059-a1fe-adef2a379fe9, N职业资格证书等级, NDT_ZYZGZSDJ, NDT_ZYZGZSDJ, N‘‘, 0)
INSERT [dbo].[P_DictionaryType] ([DT_ObjectID], [DT_Name], [DT_ShortName], [DT_Number], [DT_Introduction], [DT_IsDelete]) VALUES (N559f7c59-e710-40cd-be9e-b1582ffb059f, N入学资格, NDT_RXZG, NDT_RXZG, NULL, 0)
INSERT [dbo].[P_DictionaryType] ([DT_ObjectID], [DT_Name], [DT_ShortName], [DT_Number], [DT_Introduction], [DT_IsDelete]) VALUES (Ndfc8072d-a10d-480d-9462-bc20971bb177, N职称类别, NDT_ZCLB, NDT_ZCLB, NULL, 0)
INSERT [dbo].[P_DictionaryType] ([DT_ObjectID], [DT_Name], [DT_ShortName], [DT_Number], [DT_Introduction], [DT_IsDelete]) VALUES (Nfee41d93-4677-417f-ae20-bf989a579c7b, N学位, NXW, NDT_XW, N‘‘, 0)
INSERT [dbo].[P_DictionaryType] ([DT_ObjectID], [DT_Name], [DT_ShortName], [DT_Number], [DT_Introduction], [DT_IsDelete]) VALUES (N993d74dc-21a2-4517-b317-d9013203b6db, N论文级别, NDT_LWJB, NDT_LWJB, NULL, 0)
INSERT [dbo].[P_DictionaryType] ([DT_ObjectID], [DT_Name], [DT_ShortName], [DT_Number], [DT_Introduction], [DT_IsDelete]) VALUES (N503fe824-b190-431e-9a2d-dce0fde3f9cb, N岗位等级, NGWDJ, NDT_GWDJ, N‘‘, 0)
INSERT [dbo].[P_DictionaryType] ([DT_ObjectID], [DT_Name], [DT_ShortName], [DT_Number], [DT_Introduction], [DT_IsDelete]) VALUES (N6211fb24-7db1-4d97-9c08-ee728d2dc1f2, N团员所在领域, NDT_TYSZLY, NDT_TYSZLY, NULL, 0)
INSERT [dbo].[P_DictionaryType] ([DT_ObjectID], [DT_Name], [DT_ShortName], [DT_Number], [DT_Introduction], [DT_IsDelete]) VALUES (N657f6b25-8c32-40d2-8df9-f08df43e97bc, N毕业证书, NDT_BYZS, NDT_BYZS, N‘‘, 0)
INSERT [dbo].[P_DictionaryType] ([DT_ObjectID], [DT_Name], [DT_ShortName], [DT_Number], [DT_Introduction], [DT_IsDelete]) VALUES (Na60d6b5f-14b7-4ccf-bed6-f12663f21e4a, N处分级别, NDT_QFJB, NDT_QFJB, NULL, 0)
生成数据表脚本(字典类型表)
技术分享
USE [YZL]
GO
/****** Object:  Table [dbo].[P_Dictionary]    Script Date: 10/13/2015 14:55:35 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[P_Dictionary](
    [D_ObjectID] [uniqueidentifier] NOT NULL,
    [D_ParentID] [uniqueidentifier] NULL,
    [D_Name] [nvarchar](100) NULL,
    [D_TypeID] [uniqueidentifier] NULL,
    [D_Sort] [int] NULL,
    [D_IsDelete] [bit] NULL,
 CONSTRAINT [PK_P_DICTIONARY] PRIMARY KEY CLUSTERED 
(
    [D_ObjectID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
EXEC sys.sp_addextendedproperty @name=NMS_Description, @value=N0:已删除,1:未删除 , @level0type=NSCHEMA,@level0name=Ndbo, @level1type=NTABLE,@level1name=NP_Dictionary, @level2type=NCOLUMN,@level2name=ND_IsDelete
GO
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (N0e771708-91ab-4f39-a5fe-00cc3755cfab, N00000000-0000-0000-0000-000000000000, N1年, Ncac52b7e-1f4f-4d09-9903-8360a7750c65, 1, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (Na25a3510-f521-48b2-9b36-037f1fac965f, N00000000-0000-0000-0000-000000000000, N文学类, N5c58325f-a70b-4ab1-a0f0-1eb07aa1c14f, 3, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (N0b00d8b1-15df-4eee-86b0-07a3192b053c, N00000000-0000-0000-0000-000000000000, N统招生, Ndd3afa0d-5c46-4820-a226-95ab8fa82134, 2, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (N29f9db85-c3ef-487a-8ac7-095ede6b20db, N00000000-0000-0000-0000-000000000000, N身份证, N029c58d8-9ab3-4ef9-807c-275c3fa6dba8, 1, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (Nac909f1e-b032-4122-834f-0a7b1131e55e, N00000000-0000-0000-0000-000000000000, N高级教师资格证, Nb39e45dd-7ac2-4ab2-8241-a3fdebd9f4eb, 2, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (N1654e498-51ce-47b4-888e-16afcc6a3800, N00000000-0000-0000-0000-000000000000, N中级, N503fe824-b190-431e-9a2d-dce0fde3f9cb, 2, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (N12a12def-3276-46b1-9870-17a63f3e7c89, N00000000-0000-0000-0000-000000000000, N学生类型2, N2a800f43-13bb-4795-ab1b-5633f4eac80d, 2, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (Nd184a2a5-de48-4ed5-8705-1bde57fad5cc, N00000000-0000-0000-0000-000000000000, N教学, Nb5ff1e1c-2802-49ad-a9f0-1331ec704207, 1, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (N52153566-b386-40d2-bd82-1f3ec26a89f6, N4044496b-b928-459b-999c-9821877054c4, N车祸意外保险, N22965e30-9b7a-4c38-af54-8c4b7303f1e6, 1, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (N8a6911b6-c895-401a-9397-211864ea0aa3, N00000000-0000-0000-0000-000000000000, N健康, Na5622506-2d39-4c31-99d3-216e4c19fc77, 1, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (N4d9b53e2-46c9-40f7-a11c-24297c6d3053, N00000000-0000-0000-0000-000000000000, N厅级, N6937f4c4-0656-4ce7-8049-078b7199c26d, 3, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (Nd6fbf19d-7c27-4ef8-835f-2730d452ff8d, N00000000-0000-0000-0000-000000000000, N党员, N5e9d84b6-2cf2-4590-910e-9db30ee8126c, 1, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (N0c5aba63-3d55-4e24-8452-298fb2f316ae, N52153566-b386-40d2-bd82-1f3ec26a89f6, N大型车祸意外保险, N22965e30-9b7a-4c38-af54-8c4b7303f1e6, 2, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (Nf251f31f-e3fa-4dc0-8401-2b8c26f816bc, N00000000-0000-0000-0000-000000000000, N初级, N503fe824-b190-431e-9a2d-dce0fde3f9cb, 1, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (N2a0349be-4bce-4406-abd3-2e34b709a38d, N00000000-0000-0000-0000-000000000000, N博士, Nfee41d93-4677-417f-ae20-bf989a579c7b, 1, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (Nad5d1f6a-bd38-4a06-92ed-393e4af078a9, N00000000-0000-0000-0000-000000000000, N奖学金, N12965e30-9b7a-4c38-af54-8c4b7303f1e6, 1, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (Nbd5d1f6a-bd38-4a06-92ed-393e4af078a9, Nad5d1f6a-bd38-4a06-92ed-393e4af078a9, N国家奖学金, N12965e30-9b7a-4c38-af54-8c4b7303f1e6, 2, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (N02fd11ed-de48-42fa-a94e-3ebc42b8954c, N00000000-0000-0000-0000-000000000000, N艺术生, Ndd3afa0d-5c46-4820-a226-95ab8fa82134, 3, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (Nac8160c9-694f-411a-8396-417af30e3b8c, N00000000-0000-0000-0000-000000000000, N高级证书, Nad4fcf0a-a68c-4059-a1fe-adef2a379fe9, 1, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (N9943ced6-cd9e-4f0a-a924-4767b6e58c29, N00000000-0000-0000-0000-000000000000, N科学类, N5c58325f-a70b-4ab1-a0f0-1eb07aa1c14f, 2, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (N3447506c-4afd-4582-a6d3-4aaedc075453, N00000000-0000-0000-0000-000000000000, N正国级, N6937f4c4-0656-4ce7-8049-078b7199c26d, 1, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (Ne61168a1-2ab4-4c12-af33-5689446a353f, N00000000-0000-0000-0000-000000000000, N良好, Ne770aff5-beb5-4c41-8935-5bf1177ba405, 2, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (N075df81d-e74e-4289-b9b8-5e5e388ae2d1, N00000000-0000-0000-0000-000000000000, N高级, N503fe824-b190-431e-9a2d-dce0fde3f9cb, 3, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (N3a4d0847-77d1-4ca4-92ac-653a421d299f, N00000000-0000-0000-0000-000000000000, N学士, Nfee41d93-4677-417f-ae20-bf989a579c7b, 3, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (N901ec89e-0b57-4a08-93b1-6d1728573eb3, N00000000-0000-0000-0000-000000000000, N城市, N76c6e55e-331c-4e9a-af91-776801e1f0a6, 1, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (N53bd5b02-f2cd-4848-b1cb-73059e308eb1, N00000000-0000-0000-0000-000000000000, N部级, N6937f4c4-0656-4ce7-8049-078b7199c26d, 2, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (Nd903c5dd-01cb-46ed-aca7-753930c200dd, N00000000-0000-0000-0000-000000000000, N国家级, N6d660ae9-2c48-4ad2-abee-8e481199b04b, 1, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (N6b45fa17-a9d1-48d1-a96f-7c42e66f6f6b, N00000000-0000-0000-0000-000000000000, N学生类型1, N2a800f43-13bb-4795-ab1b-5633f4eac80d, 1, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (N044025c4-47b3-448a-8ab2-7d53996c93fd, N00000000-0000-0000-0000-000000000000, N中级, Nbf309285-b83e-404a-8baa-167b88028db5, 2, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (N762673aa-cbbb-48b0-824b-82c742c4c677, N00000000-0000-0000-0000-000000000000, N省级, N6d660ae9-2c48-4ad2-abee-8e481199b04b, 2, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (Nadef9849-9184-495a-b746-8394957a3bee, N00000000-0000-0000-0000-000000000000, N保险金, N22965e30-9b7a-4c38-af54-8c4b7303f1e6, 1, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (Nee3d6152-7857-472b-9104-843d0844f6cb, N00000000-0000-0000-0000-000000000000, N优秀, Ne770aff5-beb5-4c41-8935-5bf1177ba405, 1, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (N89150190-59f5-481b-90e5-864a3c173a01, N00000000-0000-0000-0000-000000000000, N保险金2, N22965e30-9b7a-4c38-af54-8c4b7303f1e6, 2, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (N755ef1f6-90cb-47e4-848b-899e16afd9e3, N00000000-0000-0000-0000-000000000000, N, N27927422-1e91-4ff2-aef0-8942fd30aef8, 1, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (Ne022a968-209b-4653-b65a-8bbd0a6421c6, N00000000-0000-0000-0000-000000000000, N硕士, Nfee41d93-4677-417f-ae20-bf989a579c7b, 2, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (Ne66644c4-955b-46df-b2f9-98008b3978a0, N00000000-0000-0000-0000-000000000000, N农村, N76c6e55e-331c-4e9a-af91-776801e1f0a6, 2, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (N4044496b-b928-459b-999c-9821877054c4, N89150190-59f5-481b-90e5-864a3c173a01, N意外保险, N22965e30-9b7a-4c38-af54-8c4b7303f1e6, 2, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (N85d3675a-f348-455e-b09b-abc43c7c229f, Nc5c721ec-ff78-455d-957e-bfd4bac0b256, N123, N5c58325f-a70b-4ab1-a0f0-1eb07aa1c14f, 1, 1)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (N618ed255-58a7-4f84-91e3-add92f01ae93, N89150190-59f5-481b-90e5-864a3c173a01, N社保, N22965e30-9b7a-4c38-af54-8c4b7303f1e6, 1, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (Nf37d9a13-bf8e-4518-9fb9-ae139fd68f09, N00000000-0000-0000-0000-000000000000, N患病, Na5622506-2d39-4c31-99d3-216e4c19fc77, 2, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (N4d44d992-a5a3-4e04-b85f-b10576d13262, N00000000-0000-0000-0000-000000000000, N少数民族, Nf5c29858-238c-41fe-9bc4-2ca0acec613d, 2, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (Nd7380ab8-5a7f-46ea-b206-b3e3ae1407fc, N00000000-0000-0000-0000-000000000000, N2年, Ncac52b7e-1f4f-4d09-9903-8360a7750c65, 2, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (N34b4cd54-2b58-41c3-9a49-b410932796d5, N00000000-0000-0000-0000-000000000000, N高级, Nbf309285-b83e-404a-8baa-167b88028db5, 3, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (Naabe767d-9bb0-482f-b29b-b43cb0d9aceb, N00000000-0000-0000-0000-000000000000, N团员, N5e9d84b6-2cf2-4590-910e-9db30ee8126c, 2, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (N1ad263db-8dfc-492c-a967-bd68c573c51a, N00000000-0000-0000-0000-000000000000, N本科, Nbcd1fbb8-9209-44d7-af77-9f6ecec246c0, 1, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (Nc3b34299-d1c6-4f4a-b938-bec5b76877e2, N00000000-0000-0000-0000-000000000000, N人文, N0f65f497-f371-48e3-b0b1-9226487a1c35, 2, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (Nc5c721ec-ff78-455d-957e-bfd4bac0b256, N00000000-0000-0000-0000-000000000000, N数学, N5c58325f-a70b-4ab1-a0f0-1eb07aa1c14f, 1, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (Ne25c6d63-d189-42ce-aabc-bfd9df782f7f, N00000000-0000-0000-0000-000000000000, N汉族, Nf5c29858-238c-41fe-9bc4-2ca0acec613d, 1, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (N688dffb3-ee14-45e6-9049-c0adcc64d51f, N00000000-0000-0000-0000-000000000000, N体育生, Ndd3afa0d-5c46-4820-a226-95ab8fa82134, 1, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (N00c77275-6012-4e4d-a3bd-cf648fe39353, Nadef9849-9184-495a-b746-8394957a3bee, N养老保险, N22965e30-9b7a-4c38-af54-8c4b7303f1e6, 1, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (N7ca72548-6c6b-48cb-a578-d1bdbfaeb447, N52153566-b386-40d2-bd82-1f3ec26a89f6, N小型车祸意外保险, N22965e30-9b7a-4c38-af54-8c4b7303f1e6, 1, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (Nab32b169-9793-42a9-b519-d52eb870e65a, N00000000-0000-0000-0000-000000000000, N专科, Nbcd1fbb8-9209-44d7-af77-9f6ecec246c0, 2, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (N8f2cdfda-6d1d-4abc-a169-dfddde5d2c1f, N00000000-0000-0000-0000-000000000000, N科学, N0f65f497-f371-48e3-b0b1-9226487a1c35, 1, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (N595cbdfb-75a9-4dff-bcab-ea71ee8513c3, N00000000-0000-0000-0000-000000000000, N学生证, N029c58d8-9ab3-4ef9-807c-275c3fa6dba8, 2, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (Nf7466922-882a-4290-8627-eb2956e09d71, N00000000-0000-0000-0000-000000000000, N, Nef31b612-f457-41a1-a4bc-9d416ec2a757, 1, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (N3c76a079-20c6-4c04-98e8-f518595d4ad5, N00000000-0000-0000-0000-000000000000, N后勤, Nb5ff1e1c-2802-49ad-a9f0-1331ec704207, 2, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (Ncabf7495-a5d8-4eb1-8f8e-fbc0bfea0bb2, N00000000-0000-0000-0000-000000000000, N初级, Nbf309285-b83e-404a-8baa-167b88028db5, 1, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (N7b82ea31-b1be-4d58-a13e-fcd63fe88376, N00000000-0000-0000-0000-000000000000, N中级教师资格证, Nb39e45dd-7ac2-4ab2-8241-a3fdebd9f4eb, 1, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (N3291641a-5f89-468e-99e3-fd9f8000ce35, N00000000-0000-0000-0000-000000000000, N, N27927422-1e91-4ff2-aef0-8942fd30aef8, 2, 0)
INSERT [dbo].[P_Dictionary] ([D_ObjectID], [D_ParentID], [D_Name], [D_TypeID], [D_Sort], [D_IsDelete]) VALUES (N98231747-24fa-4d40-9d55-ff8485b78ae3, N00000000-0000-0000-0000-000000000000, N, Nef31b612-f457-41a1-a4bc-9d416ec2a757, 2, 0)
生成数据表脚本(字典表)

 实现sql:

;WITH T AS  
(  
    SELECT *,CAST(CAST(D_Sort AS nvarchar)+CAST(D_ObjectID AS nvarchar(100)) as nvarchar(100)) AS px   
    FROM dbo.P_Dictionary AS A 
    WHERE NOT EXISTS(SELECT * FROM dbo.P_Dictionary WHERE D_ObjectID=A.D_ParentID) 
    UNION ALL   
    SELECT A.*,CAST(B.px+CAST(A.D_Sort AS nvarchar)+CAST(A.D_ObjectID AS nvarchar(100)) AS nvarchar(100))    
    FROM dbo.P_Dictionary AS A  
        JOIN T AS B  
           ON A.D_ParentID=B.D_ObjectID
)  
SELECT * FROM T   
ORDER BY D_TypeID, px

 

Sql Server按树形结构排序查询表记录

标签:

原文地址:http://www.cnblogs.com/feigao/p/4874533.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!