Copy query result from SQL Server Managment studio.
http://glennberrysqlperformance.spaces.live.com/Blog/cns!45041418ECCAA960!2792.entry
Daily problems and issues that are hard to resolve about SSMS,SSRS,SSIS,SSAS,DTS,Agent, Optimization, Administration, Always On, Clustering, Point in Time recovery and more...
Search This Blog & Web
Saturday, December 26, 2009
Tuesday, December 15, 2009
Using multiple CTE 's in single SQL Statement
Declare @table table (id int,name varchar(50),parentid int) Insert into @table values (1,'Director',NULL)
Insert into @table values (4,'City Manager Fsd',3)
Insert into @table values (5,'Area Manager Fsd',4)
Insert into @table values (2,'County Manager',1)
Insert into @table values (3,'Country Manager 2',1)
Insert into @table values (6,'City Manager ISB',2)
Insert into @table values (7,'Co-director',NULL)
Insert into @table values (8,'Country Head',7)
Insert into @table values (9,'Sales Manager',7)
Insert into @table values (9,'Sales Manager',4);
with CTE(id ,name,parentid,levels) as
(Select id,name,parentid ,1 as levels from @table t where parentid =1
union all
Select td.id,td.name,td.parentid,levels+ 1 as levels from @table td inner join cte on td.parentid = cte.id
),
CTE1(id ,name,parentid,levels) as
(
Select id,name,parentid ,1 as levels from @table t where parentid =7
union all
Select td.id,td.name,td.parentid,levels+ 1 as levels from @table td inner join cte on td.parentid = cte.id
)
select CTE.* from CTE inner join CTE1 on CTE.id = CTE1.id
cheers:)
Insert into @table values (4,'City Manager Fsd',3)
Insert into @table values (5,'Area Manager Fsd',4)
Insert into @table values (2,'County Manager',1)
Insert into @table values (3,'Country Manager 2',1)
Insert into @table values (6,'City Manager ISB',2)
Insert into @table values (7,'Co-director',NULL)
Insert into @table values (8,'Country Head',7)
Insert into @table values (9,'Sales Manager',7)
Insert into @table values (9,'Sales Manager',4);
with CTE(id ,name,parentid,levels) as
(Select id,name,parentid ,1 as levels from @table t where parentid =1
union all
Select td.id,td.name,td.parentid,levels+ 1 as levels from @table td inner join cte on td.parentid = cte.id
),
CTE1(id ,name,parentid,levels) as
(
Select id,name,parentid ,1 as levels from @table t where parentid =7
union all
Select td.id,td.name,td.parentid,levels+ 1 as levels from @table td inner join cte on td.parentid = cte.id
)
select CTE.* from CTE inner join CTE1 on CTE.id = CTE1.id
cheers:)
Using CTE in Sql Server 2005 / 2008
Declare @table table (id int,name varchar(50),parentid int)
Insert into @table values (1,'Director',NULL)
Insert into @table values (4,'City Manager Fsd',3)
Insert into @table values (5,'Area Manager Fsd',4)
Insert into @table values (2,'County Manager',1)
Insert into @table values (3,'Country Manager 2',1)
Insert into @table values (6,'City Manager ISB',2)
Insert into @table values (7,'Co-director',NULL)
Insert into @table values (8,'Country Head',7)
;
with CTE(id ,name,parentid,level) as
(
Select id,name,parentid ,1 as level from @table t where parentid is null
union all
Select td.id,td.name,td.parentid,level+ 1 as level from @table td inner join cte on td.parentid = cte.id
)
select * from CTE
Find answers of your questions in this advance technique of SQL Server
Insert into @table values (1,'Director',NULL)
Insert into @table values (4,'City Manager Fsd',3)
Insert into @table values (5,'Area Manager Fsd',4)
Insert into @table values (2,'County Manager',1)
Insert into @table values (3,'Country Manager 2',1)
Insert into @table values (6,'City Manager ISB',2)
Insert into @table values (7,'Co-director',NULL)
Insert into @table values (8,'Country Head',7)
;
with CTE(id ,name,parentid,level) as
(
Select id,name,parentid ,1 as level from @table t where parentid is null
union all
Select td.id,td.name,td.parentid,level+ 1 as level from @table td inner join cte on td.parentid = cte.id
)
select * from CTE
Find answers of your questions in this advance technique of SQL Server
Subscribe to:
Comments (Atom)