Hi, I have a table where two columns define a hierarchical structure: New Rx_ID. On Reorders and Change orders the new Rx_ID will appear here, and the original Rx_ID will appear in the SrcRx_ID column. On New (Active) and Discontinued orders the SrcRx_ID and the NewRx_ID will be the same.Source Rx_ID. On Reorders and Change orders the original Rx_ID will appear here, and the new Rx_ID will appear in the NewRx_ID column. On New (Active) and Discontinued orders the SrcRx_ID and the NewRx_ID will be the same.newrx_id srcrx_id event5138043 5138043 Active5192504 5138043 Reorder5543044 5192504 Reorder5919239 5543044 Reorder6408905 5919239 Reorder6553711 6408905 Reorder6762847 6553711 Reorder7353943 6762847 Reorder7503074 7353943 Change7503074 7503074 Discontinu
There will always be one "Active" record, but there might not be a "Discontinued" record.So I have tried to write a CTE that will extract the hierarchy data from the table. I have written the following:DROP TABLE #OrdersCREATE TABLE #Orders( ExpectedLevel int not null, newrx_id int null, srcrx_id int null, event varchar(10) null)INSERT INTO #Orders (ExpectedLevel,newrx_id,srcrx_id,event)SELECT 0, 5138043, 5138043, 'Active' UNION ALLSELECT 1, 5192504, 5138043, 'Reorder' UNION ALLSELECT 2, 5543044, 5192504, 'Reorder' UNION ALLSELECT 3, 5919239, 5543044, 'Reorder' UNION ALLSELECT 4, 6408905, 5919239, 'Reorder' UNION ALLSELECT 5, 6553711, 6408905, 'Reorder' UNION ALLSELECT 6, 6762847, 6553711, 'Reorder' UNION ALLSELECT 7, 7353943, 6762847, 'Reorder' UNION ALLSELECT 8, 7503074, 7353943, 'Change' UNION ALLSELECT 9, 7503074, 7503074, 'Discontinu';With OrderTree (Newrx_id,Srcrx_id,event,ExpedtedLevel,Level)as(-- Anchor member definition Select ord.Newrx_id, ord.Srcrx_id, ord.event,ord. ord.ExpectedLevel, 0 as level from #Orders as ord where event = 'Active' and ord.newrx_id = ord.Srcrx_id UNION ALL-- Recursive member definition Select ord.Newrx_id, ord.Srcrx_id, ord.event,ord. ord.ExpectedLevel, level + 1 from #Orders as ord INNER JOIN OrderTree as ot on ot.newrx_id = ord.Srcrx_id and ord.newrx_id <> ord.Srcrx_id)select * from OrderTree OPTION (MAXRECURSION 12)
In the inner join in the Recursive part of the CTE if I have the following: INNER JOIN OrderTree as ot on ot.newrx_id = ord.Srcrx_id and ord.newrx_id <> ord.Srcrx_id
I get this output (that is missing the discontinue record):Newrx_id Srcrx_id event ExpectedLevel Level5138043 5138043 Active 0 05192504 5138043 Reorder 1 15543044 5192504 Reorder 2 25919239 5543044 Reorder 3 36408905 5919239 Reorder 4 46553711 6408905 Reorder 5 56762847 6553711 Reorder 6 67353943 6762847 Reorder 7 77503074 7353943 Change 8 8
If I use this inner join: INNER JOIN OrderTree as ot on ot.newrx_id = ord.Srcrx_id
I get this:Newrx_id Srcrx_id event ExpectedLevel Level5138043 5138043 Active 0 05138043 5138043 Active 0 15192504 5138043 Reorder 1 15543044 5192504 Reorder 2 25919239 5543044 Reorder 3 36408905 5919239 Reorder 4 46553711 6408905 Reorder 5 56762847 6553711 Reorder 6 67353943 6762847 Reorder 7 77503074 7353943 Change 8 87503074 7503074 Discontinu 9 97503074 7503074 Discontinu 9 107503074 7503074 Discontinu 9 117503074 7503074 Discontinu 9 12Msg 530, Level 16, State 1, Line 1The statement terminated. The maximum recursion 12 has been exhausted before statement completion.
What can I do to fix this?Thanks,Laurie