Sunday, 27 April 2014

How to use recursive CTE calls in T-SQL

 with empcte as
 (
  select eid,name,mgrid,2 as level from employee where eid=102
  union all
  select usr.eid,usr.name,usr.mgrid,level-1 from empcte as mgr inner join employee usr on mgr.mgrid=usr.eid 
  )
  select * from empcte as u;



Given below is the employee table with its data and below that is the resultset of the above query.


No comments:

Post a Comment

C# LINQ Joins With SQL

There are  Different Types of SQL Joins  which are used to query data from more than one database tables. In this article, you will learn a...