select * from tbxx order by have_son desc,level asc
--你的排序无规律吧?

解决方案 »

  1.   

    create function f_getorder(@module_id int)
    returns varchar(8000)
    as
    begin
        declare @ret varchar(8000)
        select 
            @ret = right('0000'+module_id,4),
            @module_id = parent_id
        from 
            表
        where 
            module_id = @module_id
        
        while(@@rowcount<>0)
            select 
                @ret = right('0000'+module_id,4)+@ret,
                @module_id = parent_id
            from 
                表
            where 
                module_id = @module_id and @module_id != 0
        
        return @ret
    end 
     --调用函数
    select * from 表 order by dbo.f_getorder(module_id)
      

  2.   

    ---参考
    http://blog.csdn.net/xluzhong/articles/340928.aspx
      

  3.   

    我第一次写函数就出错,帮我再看看吧。
    create function list(@in int)
    returns @out int
    as
    begin
    set @out=@in
    return @out
    end
      

  4.   

    改为
    create function list(@in int)
    returns Int
    as
    begin 
    Declare @out Int
    set @out=@in
    return @out
    end
      

  5.   

    为什么运行时函数名前都要加dbo.  能不能不要这样?
      

  6.   

    to libin_ftsafe(子陌红尘)
    你的好象有些问题select dbo.f_getorder(module_id) from tb 
    1
    12
    13
    124
    125
    1246
    但是id好超过10好像就不行了
      

  7.   

    to paoluo(一天到晚游泳的鱼):
    如果我要返回2个Int,应该怎样写
      

  8.   

    create table 表 (module_id int,   parent_id  int ,   level  int  ,  module_name char(10)  ,  have_son int)
    insert 表 select  1     ,      0      ,      1      ,      '总公司'    ,   1
    insert 表 select  2     ,      1      ,      2      ,      '分公司1'   ,   1
    insert 表 select  3     ,      1      ,      2      ,      '分公司2'   ,   0
    insert 表 select  4     ,      2      ,      3      ,      '开发部'   ,    1
    insert 表 select  5     ,      2      ,      3      ,      '销售部'   ,    0
    insert 表 select  6     ,      4      ,      4      ,      '办公室'   ,    0
    insert 表 select  10     ,      1      ,      4      ,      '分公司3'   ,    0
    就不对了
      

  9.   

    我上面给出的函数有个BUG,修改如下:
    -----------------------------------------------------------
    alter function f_getorder(@module_id int)
    returns varchar(8000)
    as
    begin
        declare @ret varchar(8000)
        select 
            @ret = right('0000'+rtrim(module_id),4),
            @module_id = parent_id
        from 
            表
        where 
            module_id = @module_id
        
        while(@@rowcount<>0)
            select 
                @ret = right('0000'+rtrim(module_id),4)+@ret,
                @module_id = parent_id
            from 
                表
            where 
                module_id = @module_id and @module_id != 0
        
        return @ret
    end
      

  10.   

    回复人: podboq() ( ) 信誉:100  2005-04-15 12:57:00  得分: 0  
     
     
       to paoluo(一天到晚游泳的鱼):
    如果我要返回2个Int,应该怎样写
      
     
    -------------------------------------------函数不能返回两个Int,不过你可以返回一个表变量,你在表变量中给两个Int字段。
    create function list(@in int)
    returns @Tesult Table(Out1 Int,Out2 Int)
    as
    begin 
    Insert @Tesult Values(@in,@in)
    return 
    end
    GO
    Select * from dbo.list(1)