Tech Insight !

Technical blog on ASP.Net, PHP, Web Development, Web hosting , Database Programming
Home » SQL server 2005 » Writing recursive queries in SQL server 2005 using Common table expression (CTE)

Writing recursive queries in SQL server 2005 using Common table expression (CTE)

December 9th, 2009 Posted in SQL server 2005 Tags:


with
cte (empID,manID,depth,hierarchy) as

(

select e.employeeID,e.managerID ,1 , cast(e.employeeID as nvarchar(max))as hierarchy

from humanresources.Employee as e

where managerID is null


union all


select emp.employeeID,emp.managerID ,depth+1 , hierarchy + ‘/’ +cast(emp.employeeID as nvarchar(max)) as hierarchy

from humanresources.Employee emp

inner join cte

on emp.managerID=cte.empID

)


select * from cte order by depth



NOTE :

If you are in need of any Web Development feel free to Inquire us . Dhanashree Inc. Expertise in Asp.net Development, Php Development, Website designing, Open Source customisation. Dhanashree Inc can be our offshore development company / outsourcing web development company, hire dedicated web programmers.

Above information is for knowledge sharing if you have problem / issue / suggestion please intimate us with details for proper and prompt action.

2 Responses to “Writing recursive queries in SQL server 2005 using Common table expression (CTE)”

  1. [New Post] Writing recursive queries in SQL server 2005 using Common table expression (CTE) – via @twitoaster http://dhanashree.com/techblog/index.php...
    via Twitoaster

  2. nice post. thanks.

Leave a Reply