Posts

Showing posts from 2011

Recursive Query Using CTE in SQL Server

Dear Friends, here i am posting the replacement of cursors using recursive query with the use of CTE. Table Creation Scripts : - CREATE TABLE [dbo].[tb_mlm]( [ID] [int] IDENTITY(10000001,1) NOT NULL, [User_ID] [int] NULL, [PARENT_ID] [int] NULL, [poss] [tinyint] NULL ) ON [PRIMARY] GO INSERT INTO [dbo].[tb_mlm]([User_ID] ,[PARENT_ID],[poss]) VALUES(1,null,null) INSERT INTO [dbo].[tb_mlm]([User_ID] ,[PARENT_ID],[poss]) VALUES(2,1,0) INSERT INTO [dbo].[tb_mlm]([User_ID] ,[PARENT_ID],[poss]) VALUES(3,1,1) INSERT INTO [dbo].[tb_mlm]([User_ID] ,[PARENT_ID],[poss]) VALUES(4,2,0) INSERT INTO [dbo].[tb_mlm]([User_ID] ,[PARENT_ID],[poss]) VALUES(5,2,1) INSERT INTO [dbo].[tb_mlm]([User_ID] ,[PARENT_ID],[poss]) VALUES(6,3,0) INSERT INTO [dbo].[tb_mlm]([User_ID] ,[PARENT_ID],[poss]) VALUES(6,3,1) INSERT INTO [dbo].[tb_mlm]([User_ID] ,[PARENT_ID],[poss]) VALUES(7,1,0) INSERT INTO [dbo].[tb_mlm]([User_ID] ,[PARENT_ID],[poss]) VALUES(8,1,0) INSERT INTO [dbo].[tb_mlm]([User...