where are you going to use the data? HTML pages in a browser or normal client programs? are the data dynamic? can you create temporary files locally on the client machine?If your environment allows you to retrieve the data bit by bit, you only need to retrieve the first few levels in the first run, then retrieve children nodes for a specific branch when a user tries to expand it

解决方案 »

  1.   

    这和你的表结构中的父子关系的表示方法有关,简单的方法使用SQL2000的自定义函数,用Select ..... where 自定义函数=1 的方法列出,但效率可能较低一点;另一种方法是另外构造一个表,表达那棵树,比如:节点ID、父节点ID、节点名称,父节点ID=NULL表示没有父节点。在母表中用触发器保持新表的树结构一致,则可以很容易地用存储过程查询新表,返回所需要的纪录集。
      

  2.   

    各位朋友,想法我想了很多,我关心的不是思路的问题,而是如何去做的问题。
    我的想法是:
    先建立临时(temp)表,这个表,主要是描述各个节点的详细信息。
    然后根据建立的临时表,按某种遍历生成一颗树。
    最后,至于查询,可找到该点后,将其下连续的行且层次数大于该点的行的相关信息一并列出。
    不知我的想法是否有问题,请各位大侠指教!
    如果可以,怎样实现这个想法呢?这涉及的算法比较多,各位大侠可有什么好方法?
      

  3.   

    试试这个,是我在写岗位角色Role_ID与员工树使用的,给定一个角色代码,列出所属的所有下属角色及角色内的员工。没有好好整理,你先看看!CREATE PROCEDURE EEC_test @input varchar(20) 
    AS
      declare @I int
      Create Table #RST(Role_ID varchar(20))
      Create Table #Temp(RoleID varchar(20))
      insert #RST(Role_ID) select job.FatherID from EECJobRole job,EECUserRole US where job.RoleID=US.RoleID and US.User_No=@input
      insert #Temp(RoleID) select RoleID from EECUserRole where User_No=@input
      select @I=1
     While (@I<>0)
     BEGIN
      
      declare @role1 varchar(20)
      declare Mycursor cursor for select Role_ID from #RST where Role_ID not in (select RoleID from #Temp) 
      open Mycursor
      FETCH NEXT FROM mycursor INTO @role1
      
      WHILE @@FETCH_STATUS = 0
       BEGIN
           insert #RST(Role_ID) select FatherID from EECJobRole where RoleID=@role1 and FatherID<>@role1 and FatherID not in (select Role_ID from #RST )
           insert #Temp(RoleID) select @role1
           FETCH NEXT FROM mycursor INTO @role1  
       END
       CLOSE Mycursor
       DEALLOCATE Mycursor   
      
      select @I=Count(*) from EECJobRole where RoleID in (select Role_ID from #RST where Role_ID not in (select RoleID from #Temp)) and RoleID <>FatherID END
    select * from #RST t,EECJobRole job where T.Role_ID=job.RoleID
    GO
      

  4.   

    szxiebin(阿斌哥):能不能讲一下你的大致思路。
    ....................................................
    我现在很烦的是树表的生成过程和效率的问题。