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

),))

INSERT [tb]

,,N'服装' UNION ALL

,,N'男装' UNION ALL

,,N'西装' UNION ALL

,,N'全毛' UNION ALL

,,N'化纤' UNION ALL

,,N'休闲装' UNION ALL

,,N'女装' UNION ALL

,,N'套装' UNION ALL

,,N'职业装' UNION ALL

,,N'休闲装' UNION ALL

,,N'西装' UNION ALL

,,N'全毛' UNION ALL

,,N'化纤' UNION ALL

,,N'休闲装'

GO

--SELECT * FROM [tb]

-->SQL查询如下:

;WITH T AS

(

    )) 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 

    )),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
))
INSERT [tb]
,,'test1' UNION ALL
,,'test2' UNION ALL
,,'test1.1' UNION ALL
,,'test2.1' UNION ALL
,,'test1.1.1' UNION ALL
,,'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:.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,
    ) NULL,
    ) NULL,
    ) 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
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)

生成数据表脚本(字典类型表)

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,
    ) 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=N'MS_Description', @value=N'0:已删除,1:未删除' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'P_Dictionary', @level2type=N'COLUMN',@level2name=N'D_IsDelete'
GO
, )
, )
, )
, )
, )
, )
, )
, )
, )
, )
, )
, )
, )
, )
, )
, )
, )
, )
, )
, )
, )
, )
, )
, )
, )
, )
, )
, )
, )
, )
, )
, )
, )
, )
, )
, )
, )
, )
, )
, )
, )
, )
, )
, )
, )
, )
, )
, )
, )
, )
, )
, )
, )
, )
, )
, )
, )
, )
, )
, )

生成数据表脚本(字典表)

实现sql:

;WITH T AS
(
    )) )) AS px
    FROM dbo.P_Dictionary AS A
    WHERE NOT EXISTS(SELECT * FROM dbo.P_Dictionary WHERE D_ObjectID=A.D_ParentID)
    UNION ALL
    )) ))
    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