How to retrieve hierarchical data from SQL table

后端 未结 2 387
太阳男子
太阳男子 2021-01-25 18:26

I have 2 tables : T_Employees and T_Projects

Every project has different number of employees assigned. What i need to do, is to get hierarchical structure of each employ

2条回答
  •  太阳男子
    2021-01-25 19:05

    Your result datasets can be get running this query:

    declare @project int ;
    declare cur_project cursor for 
    select distinct ProjectId from T_Projects;
    
    open cur_project
    fetch next from cur_project into @project
    while @@FETCH_STATUS = 0
    begin
    select E.* from [dbo].[T_Projects] P
    inner join [dbo].[T_Employees] E
    on P.EmployeeId = E.ID
    where P.ProjectId = @project
    
    fetch next from cur_project into @project
    end
    close cur_project;
    deallocate cur_project;
    

提交回复
热议问题