要求,先按CateID排序,再按sid排序。谢谢先。id:自增量
sid:数字
CateID:字符型根分类:0000;
一级分类:0000 0000,0000 0001,0000 0002,0000 0003,0000 0004 ……
二级分类:0000 0000 0001,0000 0000 0002 ……原序:
id      sid     CateID          CateNameCn
2 0 00000000 首页
3 2 00000001 政务大厅
15 3 000000010001 政务公开
16 2 000000010002 办事指南
17 4 000000010003 社会文化
18 6 000000010004 机关建设
20 7 000000010006 文化市场
21 8 000000010007 政策法规
22 9 000000010008 公众参与
23 10 000000010009 统计数据
138 0 000000010010 省厅概况
14 11 000000010011 贵州各地
567 12 000000010012 网站动态
4 1 00000002 文化资源
5 3 00000003 文化艺术
6 4 00000004 社会文化
我用下面排序后:
ORDER BY SUBSTRING(CateID,1,LEN(CateID)-4)+RIGHT('0000'+CONVERT(VARCHAR,sid),4) ASC
得下面的结果:
id      sid     CateID          CateNameCn
2 0 00000000 首页
4 1 00000002 文化资源
138 0 000000010010 省厅概况
16 2 000000010002 办事指南
15 3 000000010001 政务公开
17 4 000000010003 社会文化
18 6 000000010004 机关建设
20 7 000000010006 文化市场
21 8 000000010007 政策法规
22 9 000000010008 公众参与
23 10 000000010009 统计数据
14 11 000000010011 贵州各地
567 12 000000010012 网站动态
3 2 00000001 政务大厅
5 3 00000003 文化艺术
6 4 00000004 社会文化
这里不符合要求的在于:
“省厅概况”到“网站动态”这一段,应该在政务大厅后面。
我理想中的式样是:
id      sid     CateID          CateNameCn
2 0 00000000 首页
4 1 00000002 文化资源
3 2 00000001 政务大厅
138 0 000000010010 省厅概况
16 2 000000010002 办事指南
15 3 000000010001 政务公开
17 4 000000010003 社会文化
18 6 000000010004 机关建设
20 7 000000010006 文化市场
21 8 000000010007 政策法规
22 9 000000010008 公众参与
23 10 000000010009 统计数据
14 11 000000010011 贵州各地
567 12 000000010012 网站动态
5 3 00000003 文化艺术
6 4 00000004 社会文化

解决方案 »

  1.   

    原序:
    id sid CateID CateNameCn
    2 0 00000000 首页
    3 2 00000001 政务大厅
    15 3 000000010001 政务公开
    16 2 000000010002 办事指南
    17 4 000000010003 社会文化
    18 6 000000010004 机关建设
    20 7 000000010006 文化市场
    21 8 000000010007 政策法规
    22 9 000000010008 公众参与
    23 10 000000010009 统计数据
    138 0 000000010010 省厅概况
    14 11 000000010011 贵州各地
    567 12 000000010012 网站动态
    4 1 00000002 文化资源
    5 3 00000003 文化艺术
    6 4 00000004 社会文化
    我用下面排序后:
    ORDER BY SUBSTRING(CateID,1,LEN(CateID)-4)+RIGHT('0000'+CONVERT(VARCHAR,sid),4) ASC
    得下面的结果:
    id sid CateID CateNameCn
    2 0 00000000 首页
    4 1 00000002 文化资源
    138 0 000000010010 省厅概况
    16 2 000000010002 办事指南
    15 3 000000010001 政务公开
    17 4 000000010003 社会文化
    18 6 000000010004 机关建设
    20 7 000000010006 文化市场
    21 8 000000010007 政策法规
    22 9 000000010008 公众参与
    23 10 000000010009 统计数据
    14 11 000000010011 贵州各地
    567 12 000000010012 网站动态
    3 2 00000001 政务大厅
    5 3 00000003 文化艺术
    6 4 00000004 社会文化
    这里不符合要求的在于:
    “省厅概况”到“网站动态”这一段,应该在政务大厅后面。
    我理想中的式样是:
    id sid CateID CateNameCn
    2 0 00000000 首页
    4 1 00000002 文化资源
    3 2 00000001 政务大厅
    138 0 000000010010 省厅概况
    16 2 000000010002 办事指南
    15 3 000000010001 政务公开
    17 4 000000010003 社会文化
    18 6 000000010004 机关建设
    20 7 000000010006 文化市场
    21 8 000000010007 政策法规
    22 9 000000010008 公众参与
    23 10 000000010009 统计数据
    14 11 000000010011 贵州各地
    567 12 000000010012 网站动态
    5 3 00000003 文化艺术
    6 4 00000004 社会文化
      

  2.   

    select *
    from table
    order by CateID,sid
      

  3.   

    这个CSDN把格式全部都过滤掉了,看得不清楚,
    这个网址就可以看了。
    http://www.gzwht.gov.cn/www.txt
      

  4.   

    http://www.gzwht.gov.cn/www.txt
      

  5.   

    对的,像rouqu说的那样,但是又要按sid排序。
      

  6.   

    select * from 表 
    order by case when CateID='00000002' then '00000000' else CateID end , sid
      

  7.   

    leo_lesley,不能把cateid固定,因为这是个9999级分类的树。
      

  8.   


    搂主:
    我理想中的式样是:
    id sid CateID CateNameCn
    2 0 00000000 首页
    4 1 00000002 文化资源
    3 2 00000001 政务大厅
    138 0 000000010010 省厅概况
    16 2 000000010002 办事指南
    15 3 000000010001 政务公开
    17 4 000000010003 社会文化
    18 6 000000010004 机关建设
    20 7 000000010006 文化市场
    21 8 000000010007 政策法规
    22 9 000000010008 公众参与
    23 10 000000010009 统计数据
    14 11 000000010011 贵州各地
    567 12 000000010012 网站动态
    5 3 00000003 文化艺术
    6 4 00000004 社会文化个人认为,正确需要的排序应该是
    我理想中的式样是:
    id sid CateID CateNameCn
    2 0 00000000 首页
    3 2 00000001 政务大厅
    138 0 000000010010 省厅概况
    16 2 000000010002 办事指南
    15 3 000000010001 政务公开
    17 4 000000010003 社会文化
    18 6 000000010004 机关建设
    20 7 000000010006 文化市场
    21 8 000000010007 政策法规
    22 9 000000010008 公众参与
    23 10 000000010009 统计数据
    14 11 000000010011 贵州各地
    567 12 000000010012 网站动态
    4 1 00000002 文化资源
    5 3 00000003 文化艺术
    6 4 00000004 社会文化
      

  9.   

    create table t(id int,sid int, CateID varchar(100),CateNameCn varchar(100))
    insert t
    select 2  ,0 ,'00000000','首页' 
    union select 4  ,1 ,'00000002','文化资源' 
    union select 3  ,2 ,'00000001','政务大厅' 
    union select 138 ,0 ,'000000010010','省厅概况' 
    union select 16  ,2 ,'000000010002','办事指南' 
    union select 15  ,3 ,'000000010001','政务公开' 
    union select 17  ,4 ,'000000010003','社会文化' 
    union select 18  ,6 ,'000000010004','机关建设' 
    union select 20  ,7 ,'000000010006','文化市场' 
    union select 21  ,8 ,'000000010007','政策法规' 
    union select 22  ,9 ,'000000010008','公众参与' 
    union select 23 ,10 ,'000000010009','统计数据' 
    union select 14 ,11 ,'000000010011','贵州各地' 
    union select 56 ,12 ,'000000010012','网站动态' 
    union select 5  ,3 ,'00000003','文化艺术 '
    union select 6  ,4 ,'00000004','社会文化'
    go
    select * from t 
    order by case when CateID='00000002' then '00000000' else CateID end , sid
    go
    drop table t/*   结果  id          sid         CateID                                                                                               CateNameCn                                                                                           
    ----------- ----------- ---------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------- 
    2           0           00000000                                                                                             首页
    4           1           00000002                                                                                             文化资源
    3           2           00000001                                                                                             政务大厅
    15          3           000000010001                                                                                         政务公开
    16          2           000000010002                                                                                         办事指南
    17          4           000000010003                                                                                         社会文化
    18          6           000000010004                                                                                         机关建设
    20          7           000000010006                                                                                         文化市场
    21          8           000000010007                                                                                         政策法规
    22          9           000000010008                                                                                         公众参与
    23          10          000000010009                                                                                         统计数据
    138         0           000000010010                                                                                         省厅概况
    14          11          000000010011                                                                                         贵州各地
    56          12          000000010012                                                                                         网站动态
    5           3           00000003                                                                                             文化艺术 
    6           4           00000004                                                                                             社会文化(所影响的行数为 16 行)
    */
      

  10.   


    id sid CateID CateNameCn
    2 0 00000000 首页
    4 1 00000002 文化资源
    3 2 00000001 政务大厅
    138 0 000000010010 省厅概况
    16 2 000000010002 办事指南
    15 3 000000010001 政务公开
    17 4 000000010003 社会文化
    18 6 000000010004 机关建设
    20 7 000000010006 文化市场
    21 8 000000010007 政策法规
    22 9 000000010008 公众参与
    23 10 000000010009 统计数据
    14 11 000000010011 贵州各地
    567 12 000000010012 网站动态
    5 3 00000003 文化艺术
    6 4 00000004 社会文化
      

  11.   

    leo_lesley,不能把cateid固定,因为这是个9999级分类的树。-----------------------------问问楼主,为什么把0000002写在0000001的前面啊!
      

  12.   

    leo_lesley同志,为什么要用CateID='00000002' then '00000000'?而不用其它的?
      

  13.   

    因为00000002的sid是1,而00000001的sid是2啊。
    id      sid     cateid          catenamecn
    4 1 00000002 文化资源
    3 2 00000001 政务大厅
      

  14.   


    declare @t table (
    id  int,
    sid int,
    CateID varchar(20),
    CateNameCn varchar(50)
    )insert @t select
    2, 0, '00000000', '首页'
    union all select 
    3, 2, '00000001', '政务大厅' 
    union all select 
    15, 3, '000000010001', '政务公开' 
    union all select 
    16, 2, '000000010002', '办事指南' 
    union all select 
    17, 4, '000000010003', '社会文化' 
    union all select 
    18, 6, '000000010004', '机关建设' 
    union all select 
    20, 7, '000000010006', '文化市场' 
    union all select 
    21, 8, '000000010007', '政策法规' 
    union all select 
    22, 9, '000000010008', '公众参与' 
    union all select 
    23, 10, '000000010009', '统计数据' 
    union all select 
    138, 0, '000000010010', '省厅概况' 
    union all select 
    14 ,11, '000000010011', '贵州各地' 
    union all select 
    567, 12, '000000010012', '网站动态' 
    union all select 
    4, 1, '00000002', '文化资源'
    union all select 
    5, 3, '00000003', '文化艺术'
    union all select 
    6, 4, '00000004', '社会文化' 
    select * from @t
    order by left(CateID,8),len(CateID),sid
    --结果
    id          sid         CateID               CateNameCn                                         
    ----------- ----------- -------------------- -------------------------------------------------- 
    2           0           00000000             首页
    3           2           00000001             政务大厅
    138         0           000000010010         省厅概况
    16          2           000000010002         办事指南
    15          3           000000010001         政务公开
    17          4           000000010003         社会文化
    18          6           000000010004         机关建设
    20          7           000000010006         文化市场
    21          8           000000010007         政策法规
    22          9           000000010008         公众参与
    23          10          000000010009         统计数据
    14          11          000000010011         贵州各地
    567         12          000000010012         网站动态
    4           1           00000002             文化资源
    5           3           00000003             文化艺术
    6           4           00000004             社会文化(所影响的行数为 16 行)
      

  15.   


    --这个符合要求了declare @t table (
    id  int,
    sid int,
    CateID varchar(20),
    CateNameCn varchar(50)
    )insert @t select
    2, 0, '00000000', '首页'
    union all select 
    3, 2, '00000001', '政务大厅' 
    union all select 
    15, 3, '000000010001', '政务公开' 
    union all select 
    16, 2, '000000010002', '办事指南' 
    union all select 
    17, 4, '000000010003', '社会文化' 
    union all select 
    18, 6, '000000010004', '机关建设' 
    union all select 
    20, 7, '000000010006', '文化市场' 
    union all select 
    21, 8, '000000010007', '政策法规' 
    union all select 
    22, 9, '000000010008', '公众参与' 
    union all select 
    23, 10, '000000010009', '统计数据' 
    union all select 
    138, 0, '000000010010', '省厅概况' 
    union all select 
    14 ,11, '000000010011', '贵州各地' 
    union all select 
    567, 12, '000000010012', '网站动态' 
    union all select 
    4, 1, '00000002', '文化资源'
    union all select 
    5, 3, '00000003', '文化艺术'
    union all select 
    6, 4, '00000004', '社会文化' 
    select * from @t a
    order by (select sid from @t where CateID=left(a.CateID,8)),left(CateID,8),len(CateID),sid--结果
    id          sid         CateID               CateNameCn                                         
    ----------- ----------- -------------------- -------------------------------------------------- 
    2           0           00000000             首页
    4           1           00000002             文化资源
    3           2           00000001             政务大厅
    138         0           000000010010         省厅概况
    16          2           000000010002         办事指南
    15          3           000000010001         政务公开
    17          4           000000010003         社会文化
    18          6           000000010004         机关建设
    20          7           000000010006         文化市场
    21          8           000000010007         政策法规
    22          9           000000010008         公众参与
    23          10          000000010009         统计数据
    14          11          000000010011         贵州各地
    567         12          000000010012         网站动态
    5           3           00000003             文化艺术
    6           4           00000004             社会文化(所影响的行数为 16 行)
      

  16.   


    --简化下declare @t table (
    id  int,
    sid int,
    CateID varchar(20),
    CateNameCn varchar(50)
    )insert @t select
    2, 0, '00000000', '首页'
    union all select 
    3, 2, '00000001', '政务大厅' 
    union all select 
    15, 3, '000000010001', '政务公开' 
    union all select 
    16, 2, '000000010002', '办事指南' 
    union all select 
    17, 4, '000000010003', '社会文化' 
    union all select 
    18, 6, '000000010004', '机关建设' 
    union all select 
    20, 7, '000000010006', '文化市场' 
    union all select 
    21, 8, '000000010007', '政策法规' 
    union all select 
    22, 9, '000000010008', '公众参与' 
    union all select 
    23, 10, '000000010009', '统计数据' 
    union all select 
    138, 0, '000000010010', '省厅概况' 
    union all select 
    14 ,11, '000000010011', '贵州各地' 
    union all select 
    567, 12, '000000010012', '网站动态' 
    union all select 
    4, 1, '00000002', '文化资源'
    union all select 
    5, 3, '00000003', '文化艺术'
    union all select 
    6, 4, '00000004', '社会文化' 
    select * from @t a
    order by (select sid from @t where CateID=left(a.CateID,8)),len(CateID),sid
    --结果
    id          sid         CateID               CateNameCn                                         
    ----------- ----------- -------------------- -------------------------------------------------- 
    2           0           00000000             首页
    4           1           00000002             文化资源
    3           2           00000001             政务大厅
    138         0           000000010010         省厅概况
    16          2           000000010002         办事指南
    15          3           000000010001         政务公开
    17          4           000000010003         社会文化
    18          6           000000010004         机关建设
    20          7           000000010006         文化市场
    21          8           000000010007         政策法规
    22          9           000000010008         公众参与
    23          10          000000010009         统计数据
    14          11          000000010011         贵州各地
    567         12          000000010012         网站动态
    5           3           00000003             文化艺术
    6           4           00000004             社会文化(所影响的行数为 16 行)
      

  17.   

    Haiwer (),的确是可以,我太崇拜你的SQL了。
      

  18.   

    create table t(id int,sid int, CateID varchar(100),CateNameCn varchar(100))
    insert t
    select 2  ,0 ,'00000000','首页' 
    union select 4  ,1 ,'00000002','文化资源' 
    union select 3  ,2 ,'00000001','政务大厅' 
    union select 138 ,0 ,'000000010010','省厅概况' 
    union select 16  ,2 ,'000000010002','办事指南' 
    union select 15  ,3 ,'000000010001','政务公开' 
    union select 17  ,4 ,'000000010003','社会文化' 
    union select 18  ,6 ,'000000010004','机关建设' 
    union select 20  ,7 ,'000000010006','文化市场' 
    union select 21  ,8 ,'000000010007','政策法规' 
    union select 22  ,9 ,'000000010008','公众参与' 
    union select 23 ,10 ,'000000010009','统计数据' 
    union select 14 ,11 ,'000000010011','贵州各地' 
    union select 56 ,12 ,'000000010012','网站动态' 
    union select 5  ,3 ,'00000003','文化艺术 '
    union select 6  ,4 ,'00000004','社会文化'
    go
    ------用个函数
    create function f(@sid int , @CateID varchar(100))
    returns varchar(8000)
    as
    begin 
    declare @str varchar(8000)
    declare @num int

    set @str=''
    if len(@CateID)=8
    set @str=rtrim(@sid)+rtrim(@CateID)
    else
    begin
    select @num=sid from t where left(@CateID,len(@CateID)-4)=CateID
    set @str=rtrim(@num)+rtrim(@CateID)
    end
    return @str
    end
    go
    select * from t 
    order by dbo.f(sid,CateID) 
    godrop table t
    drop function f
      

  19.   

    Haiwer   (),兄弟,你的那个排到后面就不行了。
    就是多有几层就不行了。leo_lesley大哥,你那个仿佛不行哦。
      

  20.   

    create table t(id int,sid int, CateID varchar(100),CateNameCn varchar(100))
    insert t
    select 2  ,0 ,'00000000','首页' 
    union select 4  ,1 ,'00000002','文化资源' 
    union select 3  ,2 ,'00000001','政务大厅' 
    union select 138 ,0 ,'000000010010','省厅概况' 
    union select 16  ,2 ,'000000010002','办事指南' 
    union select 15  ,3 ,'000000010001','政务公开' 
    union select 17  ,4 ,'000000010003','社会文化' 
    union select 18  ,6 ,'000000010004','机关建设' 
    union select 20  ,7 ,'000000010006','文化市场' 
    union select 21  ,8 ,'000000010007','政策法规' 
    union select 22  ,9 ,'000000010008','公众参与' 
    union select 23 ,10     ,'000000010009','统计数据' 
    union select 14 ,11     ,'000000010011','贵州各地' 
    union select 56 ,12     ,'000000010012','网站动态' 
    union select 5  ,3     ,'00000003','文化艺术 '
    union select 6  ,4     ,'00000004','社会文化'
    union select 172   , 0     ,'0000000100110000','贵阳市'
    union select 212   , 1     ,'0000000100110001','遵义地区'
    union select 680   , 2     ,'0000000100110002','安顺地区'
    union select 681   , 3     ,'0000000100110003','黔南州'
    union select 682   , 4     ,'0000000100110004','黔东南州'
    union select 683   , 5     ,'0000000100110005','铜仁地区'
    union select 684   , 6     ,'0000000100110006','毕节地区'
    union select 685   , 7     ,'0000000100110007','六盘水地区'
    union select 686   , 8     ,'0000000100110008','黔西南州'
    union select 215   , 0     ,'00000001001100010001','遵义市文化局'
    union select 214   , 1     ,'00000001001100010000','遵义地区文化局'
    union select 216   , 2     ,'00000001001100010002','遵义县文化局'
    union select 219   , 3     ,'00000001001100010003','赤水市文化局'
    go
    ------用个函数
    create function f(@sid int , @CateID varchar(100))
    returns varchar(8000)
    as
    begin 
        declare @str varchar(8000)
        declare @num int
        
        set @str=''
        if len(@CateID)=8
            set @str=rtrim(@sid)+rtrim(@CateID)
        else
        begin
            select @num=sid from t where left(@CateID,8)=CateID
            set @str=rtrim(@num)+rtrim(@CateID)
        end
        return @str
    endgo
    select * from t 
    order by dbo.f(sid,CateID)
    godrop table t
    drop function f
      

  21.   

    create table t(id int,sid int, CateID varchar(100),CateNameCn varchar(100))
    insert t
    select 2  ,0 ,'00000000','首页' 
    union select 4  ,1 ,'00000002','文化资源' 
    union select 3  ,2 ,'00000001','政务大厅' 
    union select 138 ,0 ,'000000010010','省厅概况' 
    union select 16  ,2 ,'000000010002','办事指南' 
    union select 15  ,3 ,'000000010001','政务公开' 
    union select 17  ,4 ,'000000010003','社会文化' 
    union select 18  ,6 ,'000000010004','机关建设' 
    union select 20  ,7 ,'000000010006','文化市场' 
    union select 21  ,8 ,'000000010007','政策法规' 
    union select 22  ,9 ,'000000010008','公众参与' 
    union select 23 ,10     ,'000000010009','统计数据' 
    union select 14 ,11     ,'000000010011','贵州各地' 
    union select 56 ,12     ,'000000010012','网站动态' 
    union select 5  ,3     ,'00000003','文化艺术 '
    union select 6  ,4     ,'00000004','社会文化'
    union select 172   , 0     ,'0000000100110000','贵阳市'
    union select 212   , 1     ,'0000000100110001','遵义地区'
    union select 680   , 2     ,'0000000100110002','安顺地区'
    union select 681   , 3     ,'0000000100110003','黔南州'
    union select 682   , 4     ,'0000000100110004','黔东南州'
    union select 683   , 5     ,'0000000100110005','铜仁地区'
    union select 684   , 6     ,'0000000100110006','毕节地区'
    union select 685   , 7     ,'0000000100110007','六盘水地区'
    union select 686   , 8     ,'0000000100110008','黔西南州'
    union select 215   , 0     ,'00000001001100010001','遵义市文化局'
    union select 214   , 1     ,'00000001001100010000','遵义地区文化局'
    union select 216   , 2     ,'00000001001100010002','遵义县文化局'
    union select 219   , 3     ,'00000001001100010003','赤水市文化局'
    go
    ------用个函数
    create function f(@sid int , @CateID varchar(100))
    returns varchar(8000)
    as
    begin 
        declare @str varchar(8000)
        declare @num int
        
        set @str=''
        if len(@CateID)=8
            set @str=rtrim(@sid)+rtrim(@CateID)
        else
        begin
            select @num=sid from t where left(@CateID,8)=CateID
            set @str=rtrim(@num)+rtrim(@CateID)
        end
        return @str
    endgo
    select * from t 
    order by dbo.f(sid,CateID)
    godrop table t
    drop function f
      

  22.   

    leo哥,经过测试,你那个按cateid的处理还是很成功的,但是那个sid排序没有成功。
      

  23.   

    比如说:
    union select 172   , 0     ,'0000000100110000','贵阳市'
    union select 212   , 1     ,'0000000100110001','遵义地区'
    union select 680   , 2     ,'0000000100110002','安顺地区'
    union select 681   , 3     ,'0000000100110003','黔南州'
    union select 682   , 4     ,'0000000100110004','黔东南州'
    union select 683   , 5     ,'0000000100110005','铜仁地区'
    union select 684   , 6     ,'0000000100110006','毕节地区'
    union select 685   , 7     ,'0000000100110007','六盘水地区'
    union select 686   , 8     ,'0000000100110008','黔西南州'这几行应该排在“贵州各地”后面的。
      

  24.   

    create table t(id int,sid int, CateID varchar(100),CateNameCn varchar(100))
    insert t
    select 2  ,0 ,'00000000','首页' 
    union select 4  ,1 ,'00000002','文化资源' 
    union select 3  ,2 ,'00000001','政务大厅' 
    union select 138 ,0 ,'000000010010','省厅概况' 
    union select 16  ,2 ,'000000010002','办事指南' 
    union select 15  ,3 ,'000000010001','政务公开' 
    union select 17  ,4 ,'000000010003','社会文化' 
    union select 18  ,6 ,'000000010004','机关建设' 
    union select 20  ,7 ,'000000010006','文化市场' 
    union select 21  ,8 ,'000000010007','政策法规' 
    union select 22  ,9 ,'000000010008','公众参与' 
    union select 23 ,10     ,'000000010009','统计数据' 
    union select 14 ,11     ,'000000010011','贵州各地' 
    union select 56 ,12     ,'000000010012','网站动态' 
    union select 5  ,3     ,'00000003','文化艺术 '
    union select 6  ,4     ,'00000004','社会文化'
    union select 172   , 0     ,'0000000100110000','贵阳市'
    union select 212   , 1     ,'0000000100110001','遵义地区'
    union select 680   , 2     ,'0000000100110002','安顺地区'
    union select 681   , 3     ,'0000000100110003','黔南州'
    union select 682   , 4     ,'0000000100110004','黔东南州'
    union select 683   , 5     ,'0000000100110005','铜仁地区'
    union select 684   , 6     ,'0000000100110006','毕节地区'
    union select 685   , 7     ,'0000000100110007','六盘水地区'
    union select 686   , 8     ,'0000000100110008','黔西南州'
    union select 215   , 0     ,'00000001001100010001','遵义市文化局'
    union select 214   , 1     ,'00000001001100010000','遵义地区文化局'
    union select 216   , 2     ,'00000001001100010002','遵义县文化局'
    union select 219   , 3     ,'00000001001100010003','赤水市文化局'
    go
    ------用个函数
    create function f(@sid int , @CateID varchar(100))
    returns varchar(8000)
    as
    begin 
        declare @str varchar(8000)
        declare @num varchar(10)
        declare @s varchar(10)
        declare @len int,@start int
        
        select @str='',@s='',@num='',@len=9,@start=9
        if len(@CateID)=8
            set @str=right('000'+rtrim(@sid),3)+rtrim(@CateID)
        else
        begin
          select @num=right('000'+rtrim(sid),3) from t where left(@CateID,8)=CateID
           set @str=rtrim(@num)+rtrim(@CateID)
      set @s=substring(@str,@len,7)
    while len(@s)>0
    begin
    select @start=@len
    select @len=@len+7
    select @num=right('000'+rtrim(sid),3) from t where left(@str,@len)=CateID
             set @str=stuff(@str,@start,0,@num)
    select @s=substring(@str,@len,7)
    end
    set @str=stuff(@str,len(@str)-4,0,@sid)
        end
        return @str
    endgo
    select dbo.f(sid,CateID),* from t 
    order by dbo.f(sid,CateID)
    godrop table t
    drop function f
      

  25.   


    --看看这个结果是否符合要求declare @t table(id int,sid int, CateID varchar(100),CateNameCn varchar(100))
    insert @t
    select 2     ,0 ,'00000000','首页' 
    union select 4     ,1 ,'00000002','文化资源' 
    union select 3     ,2 ,'00000001','政务大厅' 
    union select 138    ,0 ,'000000010010','省厅概况' 
    union select 16     ,2 ,'000000010002','办事指南' 
    union select 15     ,3 ,'000000010001','政务公开' 
    union select 17     ,4 ,'000000010003','社会文化' 
    union select 18     ,6 ,'000000010004','机关建设' 
    union select 20     ,7 ,'000000010006','文化市场' 
    union select 21     ,8 ,'000000010007','政策法规' 
    union select 22     ,9 ,'000000010008','公众参与' 
    union select 23    ,10        ,'000000010009','统计数据' 
    union select 14    ,11        ,'000000010011','贵州各地' 
    union select 56    ,12        ,'000000010012','网站动态' 
    union select 5     ,3        ,'00000003','文化艺术 '
    union select 6     ,4        ,'00000004','社会文化'
    union select 172   , 0     ,'0000000100110000','贵阳市'
    union select 212   , 1     ,'0000000100110001','遵义地区'
    union select 680   , 2     ,'0000000100110002','安顺地区'
    union select 681   , 3     ,'0000000100110003','黔南州'
    union select 682   , 4     ,'0000000100110004','黔东南州'
    union select 683   , 5     ,'0000000100110005','铜仁地区'
    union select 684   , 6     ,'0000000100110006','毕节地区'
    union select 685   , 7     ,'0000000100110007','六盘水地区'
    union select 686   , 8     ,'0000000100110008','黔西南州'
    union select 215   , 0     ,'00000001001100010001','遵义市文化局'
    union select 214   , 1     ,'00000001001100010000','遵义地区文化局'
    union select 216   , 2     ,'00000001001100010002','遵义县文化局'
    union select 219   , 3     ,'00000001001100010003','赤水市文化局'
    select * from @t a
    order by (select sid from @t where CateID=left(a.CateID,8)),
    case when len(CateID)>=12 then (select sid from @t where CateID=left(a.CateID,12)) else -1 end,
    case when len(CateID)>=16 then (select sid from @t where CateID=left(a.CateID,16)) else -1 end,
    sid--结果
    id          sid         CateID                                                                                               CateNameCn                                                                                           
    ----------- ----------- ---------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------- 
    2           0           00000000                                                                                             首页
    4           1           00000002                                                                                             文化资源
    3           2           00000001                                                                                             政务大厅
    138         0           000000010010                                                                                         省厅概况
    16          2           000000010002                                                                                         办事指南
    15          3           000000010001                                                                                         政务公开
    17          4           000000010003                                                                                         社会文化
    18          6           000000010004                                                                                         机关建设
    20          7           000000010006                                                                                         文化市场
    21          8           000000010007                                                                                         政策法规
    22          9           000000010008                                                                                         公众参与
    23          10          000000010009                                                                                         统计数据
    14          11          000000010011                                                                                         贵州各地
    172         0           0000000100110000                                                                                     贵阳市
    215         0           00000001001100010001                                                                                 遵义市文化局
    212         1           0000000100110001                                                                                     遵义地区
    214         1           00000001001100010000                                                                                 遵义地区文化局
    216         2           00000001001100010002                                                                                 遵义县文化局
    219         3           00000001001100010003                                                                                 赤水市文化局
    680         2           0000000100110002                                                                                     安顺地区
    681         3           0000000100110003                                                                                     黔南州
    682         4           0000000100110004                                                                                     黔东南州
    683         5           0000000100110005                                                                                     铜仁地区
    684         6           0000000100110006                                                                                     毕节地区
    685         7           0000000100110007                                                                                     六盘水地区
    686         8           0000000100110008                                                                                     黔西南州
    56          12          000000010012                                                                                         网站动态
    5           3           00000003                                                                                             文化艺术 
    6           4           00000004                                                                                             社会文化(所影响的行数为 29 行)
      

  26.   

    create table t(id int,sid int, CateID varchar(100),CateNameCn varchar(100))
    insert t
    select 2     ,0 ,'00000000','首页' 
    union select 4     ,1 ,'00000002','文化资源' 
    union select 3     ,2 ,'00000001','政务大厅' 
    union select 138    ,0 ,'000000010010','省厅概况' 
    union select 16     ,2 ,'000000010002','办事指南' 
    union select 15     ,3 ,'000000010001','政务公开' 
    union select 17     ,4 ,'000000010003','社会文化' 
    union select 18     ,6 ,'000000010004','机关建设' 
    union select 20     ,7 ,'000000010006','文化市场' 
    union select 21     ,8 ,'000000010007','政策法规' 
    union select 22     ,9 ,'000000010008','公众参与' 
    union select 23    ,10        ,'000000010009','统计数据' 
    union select 14    ,11        ,'000000010011','贵州各地' 
    union select 56    ,12        ,'000000010012','网站动态' 
    union select 5     ,3        ,'00000003','文化艺术 '
    union select 6     ,4        ,'00000004','社会文化'
    union select 172   , 0     ,'0000000100110000','贵阳市'
    union select 212   , 1     ,'0000000100110001','遵义地区'
    union select 680   , 2     ,'0000000100110002','安顺地区'
    union select 681   , 3     ,'0000000100110003','黔南州'
    union select 682   , 4     ,'0000000100110004','黔东南州'
    union select 683   , 5     ,'0000000100110005','铜仁地区'
    union select 684   , 6     ,'0000000100110006','毕节地区'
    union select 685   , 7     ,'0000000100110007','六盘水地区'
    union select 686   , 8     ,'0000000100110008','黔西南州'
    union select 215   , 0     ,'00000001001100010001','遵义市文化局'
    union select 214   , 1     ,'00000001001100010000','遵义地区文化局'
    union select 216   , 2     ,'00000001001100010002','遵义县文化局'
    union select 219   , 3     ,'00000001001100010003','赤水市文化局'
    go
    ------用个函数
    create function f(@sid int , @CateID varchar(100))
    returns varchar(8000)
    as
    begin 
        declare @str varchar(8000)
        declare @num varchar(10)
        declare @s varchar(10)
        declare @len int,@start int,@end int
        
        select @str='',@s='',@num='',@len=12,@start=12,@end=9
        if len(@CateID)=8
            set @str=right('000'+rtrim(@sid),3)+rtrim(@CateID)
        else
        begin
         select @num=right('000'+rtrim(sid),3) from t where left(@CateID,8)=CateID
            set @str=rtrim(@num)+rtrim(@CateID)
    set @s=substring(@CateID,9,4)
         while len(@s)>0
          begin    
               select @num=right('000'+rtrim(sid),3) from t where left(@CateID,@start)=CateID
                  set @str=stuff(@str,@len,0,@num)
    select @len=@len+8,@start=@start+4,@end=@end+4
              select @s=substring(@CateID,@end,4)
          end
        end
        return @str
    endgo
    select dbo.f(sid,CateID),* from t 
    order by dbo.f(sid,CateID)
    godrop table t
    drop function f
      

  27.   


    --少写了一层declare @t table(id int,sid int, CateID varchar(40),CateNameCn varchar(100))
    insert @t
    select 2     ,0 ,'00000000','首页' 
    union select 4     ,1 ,'00000002','文化资源' 
    union select 3     ,2 ,'00000001','政务大厅' 
    union select 138    ,0 ,'000000010010','省厅概况' 
    union select 16     ,2 ,'000000010002','办事指南' 
    union select 15     ,3 ,'000000010001','政务公开' 
    union select 17     ,4 ,'000000010003','社会文化' 
    union select 18     ,6 ,'000000010004','机关建设' 
    union select 20     ,7 ,'000000010006','文化市场' 
    union select 21     ,8 ,'000000010007','政策法规' 
    union select 22     ,9 ,'000000010008','公众参与' 
    union select 23    ,10        ,'000000010009','统计数据' 
    union select 14    ,11        ,'000000010011','贵州各地' 
    union select 56    ,12        ,'000000010012','网站动态' 
    union select 5     ,3        ,'00000003','文化艺术 '
    union select 6     ,4        ,'00000004','社会文化'
    union select 172   , 0     ,'0000000100110000','贵阳市'
    union select 212   , 1     ,'0000000100110001','遵义地区'
    union select 680   , 2     ,'0000000100110002','安顺地区'
    union select 681   , 3     ,'0000000100110003','黔南州'
    union select 682   , 4     ,'0000000100110004','黔东南州'
    union select 683   , 5     ,'0000000100110005','铜仁地区'
    union select 684   , 6     ,'0000000100110006','毕节地区'
    union select 685   , 7     ,'0000000100110007','六盘水地区'
    union select 686   , 8     ,'0000000100110008','黔西南州'
    union select 215   , 0     ,'00000001001100010001','遵义市文化局'
    union select 214   , 1     ,'00000001001100010000','遵义地区文化局'
    union select 216   , 2     ,'00000001001100010002','遵义县文化局'
    union select 219   , 3     ,'00000001001100010003','赤水市文化局'
    select * from @t a
    order by (select sid from @t where CateID=left(a.CateID,8)),
    case when len(CateID)>=12 then (select sid from @t where CateID=left(a.CateID,12)) else -1 end,
    case when len(CateID)>=16 then (select sid from @t where CateID=left(a.CateID,16)) else -1 end,
    case when len(CateID)>=20 then (select sid from @t where CateID=left(a.CateID,20)) else -1 end,
    sid--结果
    id          sid         CateID                                   CateNameCn                                                                                           
    ----------- ----------- ---------------------------------------- ---------------------------------------------------------------------------------------------------- 
    2           0           00000000                                 首页
    4           1           00000002                                 文化资源
    3           2           00000001                                 政务大厅
    138         0           000000010010                             省厅概况
    16          2           000000010002                             办事指南
    15          3           000000010001                             政务公开
    17          4           000000010003                             社会文化
    18          6           000000010004                             机关建设
    20          7           000000010006                             文化市场
    21          8           000000010007                             政策法规
    22          9           000000010008                             公众参与
    23          10          000000010009                             统计数据
    14          11          000000010011                             贵州各地
    172         0           0000000100110000                         贵阳市
    212         1           0000000100110001                         遵义地区
    215         0           00000001001100010001                     遵义市文化局
    214         1           00000001001100010000                     遵义地区文化局
    216         2           00000001001100010002                     遵义县文化局
    219         3           00000001001100010003                     赤水市文化局
    680         2           0000000100110002                         安顺地区
    681         3           0000000100110003                         黔南州
    682         4           0000000100110004                         黔东南州
    683         5           0000000100110005                         铜仁地区
    684         6           0000000100110006                         毕节地区
    685         7           0000000100110007                         六盘水地区
    686         8           0000000100110008                         黔西南州
    56          12          000000010012                             网站动态
    5           3           00000003                                 文化艺术 
    6           4           00000004                                 社会文化(所影响的行数为 29 行)
      

  28.   

    create table t(id int,sid int, CateID varchar(100),CateNameCn varchar(100))
    insert t
    select 2     ,0 ,'00000000','首页' 
    union select 4     ,1 ,'00000002','文化资源' 
    union select 3     ,2 ,'00000001','政务大厅' 
    union select 138    ,0 ,'000000010010','省厅概况' 
    union select 16     ,2 ,'000000010002','办事指南' 
    union select 15     ,3 ,'000000010001','政务公开' 
    union select 17     ,4 ,'000000010003','社会文化' 
    union select 18     ,6 ,'000000010004','机关建设' 
    union select 20     ,7 ,'000000010006','文化市场' 
    union select 21     ,8 ,'000000010007','政策法规' 
    union select 22     ,9 ,'000000010008','公众参与' 
    union select 23    ,10        ,'000000010009','统计数据' 
    union select 14    ,11        ,'000000010011','贵州各地' 
    union select 56    ,12        ,'000000010012','网站动态' 
    union select 5     ,3        ,'00000003','文化艺术 '
    union select 6     ,4        ,'00000004','社会文化'
    union select 172   , 0     ,'0000000100110000','贵阳市'
    union select 212   , 1     ,'0000000100110001','遵义地区'
    union select 680   , 2     ,'0000000100110002','安顺地区'
    union select 681   , 3     ,'0000000100110003','黔南州'
    union select 682   , 4     ,'0000000100110004','黔东南州'
    union select 683   , 5     ,'0000000100110005','铜仁地区'
    union select 684   , 6     ,'0000000100110006','毕节地区'
    union select 685   , 7     ,'0000000100110007','六盘水地区'
    union select 686   , 8     ,'0000000100110008','黔西南州'
    union select 215   , 0     ,'00000001001100010001','遵义市文化局'
    union select 214   , 1     ,'00000001001100010000','遵义地区文化局'
    union select 216   , 2     ,'00000001001100010002','遵义县文化局'
    union select 219   , 3     ,'00000001001100010003','赤水市文化局'
    go
    ------用个函数
    create function f(@sid int , @CateID varchar(100))
    returns varchar(8000)
    as
    begin 
        declare @str varchar(8000)
        declare @num varchar(10)
        declare @s varchar(10)
        declare @len int,@start int,@end int
        
        select @str='',@s='',@num='',@len=12,@start=12,@end=9
        if len(@CateID)=8
            set @str=right('000'+rtrim(@sid),3)+rtrim(@CateID)
        else
        begin
         select @num=right('000'+rtrim(sid),3) from t where left(@CateID,8)=CateID
            set @str=rtrim(@num)+rtrim(@CateID)
    set @s=substring(@CateID,9,4)
         while len(@s)>0
          begin    
               select @num=right('000'+rtrim(sid),3) from t where left(@CateID,@start)=CateID
                  set @str=stuff(@str,@len,0,@num)
    select @len=@len+8,@start=@start+4,@end=@end+4
              select @s=substring(@CateID,@end,4)
          end
        end
        return @str
    endgo
    select dbo.f(sid,CateID),* from t 
    order by dbo.f(sid,CateID)
    godrop table t
    drop function f
    /*    结果
    00000000000 2 0 00000000 首页
    00100000002 4 1 00000002 文化资源
    00200000001 3 2 00000001 政务大厅
    002000000010000010 138 0 000000010010 省厅概况
    002000000010020002 16 2 000000010002 办事指南
    002000000010030001 15 3 000000010001 政务公开
    002000000010040003 17 4 000000010003 社会文化
    002000000010060004 18 6 000000010004 机关建设
    002000000010070006 20 7 000000010006 文化市场
    002000000010080007 21 8 000000010007 政策法规
    002000000010090008 22 9 000000010008 公众参与
    002000000010100009 23 10 000000010009 统计数据
    002000000010110011 14 11 000000010011 贵州各地
    0020000000101100110000000 172 0 0000000100110000 贵阳市
    0020000000101100110001001 212 1 0000000100110001 遵义地区
    00200000001011001100010010000001 215 0 00000001001100010001 遵义市文化局
    00200000001011001100010010000100 214 1 00000001001100010000 遵义地区文化局
    00200000001011001100010010000202 216 2 00000001001100010002 遵义县文化局
    00200000001011001100010010000303 219 3 00000001001100010003 赤水市文化局
    0020000000101100110002002 680 2 0000000100110002 安顺地区
    0020000000101100110003003 681 3 0000000100110003 黔南州
    0020000000101100110004004 682 4 0000000100110004 黔东南州
    0020000000101100110005005 683 5 0000000100110005 铜仁地区
    0020000000101100110006006 684 6 0000000100110006 毕节地区
    0020000000101100110007007 685 7 0000000100110007 六盘水地区
    0020000000101100110008008 686 8 0000000100110008 黔西南州
    002000000010120012 56 12 000000010012 网站动态
    00300000003 5 3 00000003 文化艺术 
    00400000004 6 4 00000004 社会文化*/
      

  29.   

    create table t(id int,sid int, CateID varchar(100),CateNameCn varchar(100))
    insert t
    select 2     ,0 ,'00000000','首页' 
    union select 4     ,1 ,'00000002','文化资源' 
    union select 3     ,2 ,'00000001','政务大厅' 
    union select 138    ,0 ,'000000010010','省厅概况' 
    union select 16     ,2 ,'000000010002','办事指南' 
    union select 15     ,3 ,'000000010001','政务公开' 
    union select 17     ,4 ,'000000010003','社会文化' 
    union select 18     ,6 ,'000000010004','机关建设' 
    union select 20     ,7 ,'000000010006','文化市场' 
    union select 21     ,8 ,'000000010007','政策法规' 
    union select 22     ,9 ,'000000010008','公众参与' 
    union select 23    ,10        ,'000000010009','统计数据' 
    union select 14    ,11        ,'000000010011','贵州各地' 
    union select 56    ,12        ,'000000010012','网站动态' 
    union select 5     ,3        ,'00000003','文化艺术 '
    union select 6     ,4        ,'00000004','社会文化'
    union select 172   , 0     ,'0000000100110000','贵阳市'
    union select 212   , 1     ,'0000000100110001','遵义地区'
    union select 680   , 2     ,'0000000100110002','安顺地区'
    union select 681   , 3     ,'0000000100110003','黔南州'
    union select 682   , 4     ,'0000000100110004','黔东南州'
    union select 683   , 5     ,'0000000100110005','铜仁地区'
    union select 684   , 6     ,'0000000100110006','毕节地区'
    union select 685   , 7     ,'0000000100110007','六盘水地区'
    union select 686   , 8     ,'0000000100110008','黔西南州'
    union select 215   , 0     ,'00000001001100010001','遵义市文化局'
    union select 214   , 1     ,'00000001001100010000','遵义地区文化局'
    union select 216   , 2     ,'00000001001100010002','遵义县文化局'
    union select 219   , 3     ,'00000001001100010003','赤水市文化局'
    go
    ------用个函数
    create function f(@sid int , @CateID varchar(100))
    returns varchar(8000)
    as
    begin 
        declare @str varchar(8000)
        declare @num varchar(10)
        declare @s varchar(10)
        declare @len int,@start int,@end int
        
        select @str='',@s='',@num='',@len=12,@start=12,@end=9
        if len(@CateID)=8
            set @str=right('000'+rtrim(@sid),3)+rtrim(@CateID)
        else
        begin
         select @num=right('000'+rtrim(sid),3) from t where left(@CateID,8)=CateID
            set @str=rtrim(@num)+rtrim(@CateID)
    set @s=substring(@CateID,9,4)
         while len(@s)>0
          begin    
               select @num=right('000'+rtrim(sid),3) from t where left(@CateID,@start)=CateID
                  set @str=stuff(@str,@len,0,@num)
    select @len=@len+8,@start=@start+4,@end=@end+4
              select @s=substring(@CateID,@end,4)
          end
        end
        return @str
    endgo
    select dbo.f(sid,CateID),* from t 
    order by dbo.f(sid,CateID)
    godrop table t
    drop function f
    /*    结果
    00000000000 2 0 00000000 首页
    00100000002 4 1 00000002 文化资源
    00200000001 3 2 00000001 政务大厅
    002000000010000010 138 0 000000010010 省厅概况
    002000000010020002 16 2 000000010002 办事指南
    002000000010030001 15 3 000000010001 政务公开
    002000000010040003 17 4 000000010003 社会文化
    002000000010060004 18 6 000000010004 机关建设
    002000000010070006 20 7 000000010006 文化市场
    002000000010080007 21 8 000000010007 政策法规
    002000000010090008 22 9 000000010008 公众参与
    002000000010100009 23 10 000000010009 统计数据
    002000000010110011 14 11 000000010011 贵州各地
    0020000000101100110000000 172 0 0000000100110000 贵阳市
    0020000000101100110001001 212 1 0000000100110001 遵义地区
    00200000001011001100010010000001 215 0 00000001001100010001 遵义市文化局
    00200000001011001100010010000100 214 1 00000001001100010000 遵义地区文化局
    00200000001011001100010010000202 216 2 00000001001100010002 遵义县文化局
    00200000001011001100010010000303 219 3 00000001001100010003 赤水市文化局
    0020000000101100110002002 680 2 0000000100110002 安顺地区
    0020000000101100110003003 681 3 0000000100110003 黔南州
    0020000000101100110004004 682 4 0000000100110004 黔东南州
    0020000000101100110005005 683 5 0000000100110005 铜仁地区
    0020000000101100110006006 684 6 0000000100110006 毕节地区
    0020000000101100110007007 685 7 0000000100110007 六盘水地区
    0020000000101100110008008 686 8 0000000100110008 黔西南州
    002000000010120012 56 12 000000010012 网站动态
    00300000003 5 3 00000003 文化艺术 
    00400000004 6 4 00000004 社会文化*/
      

  30.   

    按最多的层次写order by 中的个数
    也许前面层次需要区分,就改成select * from @t a
    order by (select sid from @t where CateID=left(a.CateID,4)),
    case when len(CateID)>=8 then (select sid from @t where CateID=left(a.CateID,8)) else -1 end,
    case when len(CateID)>=12 then (select sid from @t where CateID=left(a.CateID,12)) else -1 end,
    case when len(CateID)>=16 then (select sid from @t where CateID=left(a.CateID,16)) else -1 end,
    case when len(CateID)>=20 then (select sid from @t where CateID=left(a.CateID,20)) else -1 end,
    ...           --这里需要写多少个按照你的最高层次来写
    sid