Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
sadiqmodan
Starting Member
16 Posts |
Posted - 2011-03-09 : 08:18:55
|
Hello,I have data as like belowmainID parentid VirtualID Text====== ======== ========= ====1 0 5 Node12 1 0 Node1.13 1 0 Node1.24 0 9 Node25 4 0 Node2.16 4 0 Node2.27 0 0 Node38 7 6 Node3.19 7 0 Node3.2mainId: means NodeIdVirtualID: 0 means Real node, >0 meand virtual nodee.g. first record represent Node1 have no parent and it is virtual of the Node2.1when I Delete Node2.1 then I have to delete below records.mainID parentid VirtualID Text====== ======== ========= ====1 0 5 Node12 1 0 Node1.13 1 0 Node1.25 4 0 Node2.1because Node1 is virtual of Node2.1 and Node1 have 2 child. for this I have Implemented below query.================================================declare @tmp int = 5;WITH CTEAS( SELECT parentid, mainid, virtualid, LVL = RIGHT('000' + CONVERT(varchar(MAX), mainid), 3) from tblTree where mainid = @tmp or virtualid = @tmp UNION ALL SELECT tblTree.parentid, tblTree.mainid, tblTree.virtualid, LVL = CTE.lvl + RIGHT('000' + CONVERT(varchar(MAX), tblTree.mainid), 3) from CTE inner join tblTree on (tblTree.parentid = CTE.mainid) OR (tblTree.mainid = CTE.virtualid and tblTree.parentid = CTE.virtualid) ), CTE2AS( SELECT tblTree.parentid, tblTree.mainid, tblTree.virtualid, LVL = RIGHT('000' + CONVERT(varchar(MAX), tblTree.mainid), 3) from tblTree inner join CTE on tblTree.virtualid = CTE.mainid UNION ALL SELECT tblTree.parentid, tblTree.mainid, tblTree.virtualid, LVL = CTE2.lvl + RIGHT('000' + CONVERT(varchar(MAX), tblTree.mainid), 3) from tblTree inner join CTE2 on tblTree.parentid = CTE2.mainid or tblTree.virtualid = CTE2.mainid)select distinct mainid, parentid, virtualid, LVL from CTEunion allselect distinct mainid, parentid, virtualid, LVL from CTE2================================================but when I have record as belowmainID parentid VirtualID1 0 52 1 03 1 04 0 95 4 06 4 07 0 128 7 69 7 010 8 0 Node3.1.111 8 0 Node3.1.212 10 0 Node3.1.2.1and @tmp = 10 then I get the error like----------------------------------------------The statement terminated. The maximum recursion 100 has been exhausted before statement completion.----------------------------------------------at the end of query I have added below lineoption (maxrecursion 5000)but still it will gives error.So please can any have advance solution.Thanks in Advance |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-03-09 : 12:14:04
|
try usingOPTION (MAXRECURSION 0)on bottom of query------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
sadiqmodan
Starting Member
16 Posts |
Posted - 2011-03-10 : 02:29:07
|
Hi Visakh,Can you please re write sql for above..I tried with multiple sql but still no luck..I implemented max recursion as you suggested but it goes for long time and no responseThanks in advance for your help.. |
 |
|
matty
Posting Yak Master
161 Posts |
Posted - 2011-03-11 : 04:21:42
|
The issue is with recursive part in CTE2.quote: Originally posted by sadiqmodan Hello,I have data as like belowmainID parentid VirtualID Text====== ======== ========= ====1 0 5 Node12 1 0 Node1.13 1 0 Node1.24 0 9 Node25 4 0 Node2.16 4 0 Node2.27 0 0 Node38 7 6 Node3.19 7 0 Node3.2mainId: means NodeIdVirtualID: 0 means Real node, >0 meand virtual nodee.g. first record represent Node1 have no parent and it is virtual of the Node2.1when I Delete Node2.1 then I have to delete below records.mainID parentid VirtualID Text====== ======== ========= ====1 0 5 Node12 1 0 Node1.13 1 0 Node1.25 4 0 Node2.1because Node1 is virtual of Node2.1 and Node1 have 2 child. for this I have Implemented below query.================================================declare @tmp int = 5;WITH CTEAS( SELECT parentid, mainid, virtualid, LVL = RIGHT('000' + CONVERT(varchar(MAX), mainid), 3) from tblTree where mainid = @tmp or virtualid = @tmp UNION ALL SELECT tblTree.parentid, tblTree.mainid, tblTree.virtualid, LVL = CTE.lvl + RIGHT('000' + CONVERT(varchar(MAX), tblTree.mainid), 3) from CTE inner join tblTree on (tblTree.parentid = CTE.mainid) OR (tblTree.mainid = CTE.virtualid and tblTree.parentid = CTE.virtualid) ), CTE2AS( SELECT tblTree.parentid, tblTree.mainid, tblTree.virtualid, LVL = RIGHT('000' + CONVERT(varchar(MAX), tblTree.mainid), 3) from tblTree inner join CTE on tblTree.virtualid = CTE.mainid UNION ALL SELECT tblTree.parentid, tblTree.mainid, tblTree.virtualid, LVL = CTE2.lvl + RIGHT('000' + CONVERT(varchar(MAX), tblTree.mainid), 3) from tblTree inner join CTE2 on (tblTree.parentid = CTE2.mainid or tblTree.virtualid = CTE2.mainid) AND CTE2.mainID <> @tmp)select distinct mainid, parentid, virtualid, LVL from CTEunion allselect distinct mainid, parentid, virtualid, LVL from CTE2================================================but when I have record as belowmainID parentid VirtualID1 0 52 1 03 1 04 0 95 4 06 4 07 0 128 7 69 7 010 8 0 Node3.1.111 8 0 Node3.1.212 10 0 Node3.1.2.1and @tmp = 10 then I get the error like----------------------------------------------The statement terminated. The maximum recursion 100 has been exhausted before statement completion.----------------------------------------------at the end of query I have added below lineoption (maxrecursion 5000)but still it will gives error.So please can any have advance solution.Thanks in Advance
|
 |
|
|
|
|
|
|