MS SQL Recursion problem

Please support our MS SQL advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Nov 2009
Posts: 6
Reputation: elauri is an unknown quantity at this point 
Solved Threads: 0
elauri elauri is offline Offline
Newbie Poster

MS SQL Recursion problem

 
0
  #1
25 Days Ago
I have following tables
table1
nr_key1
nr_dad1

table2
nr_key2
value
(repeated ~n times with same nr_key2 and different value)

in table2 nr_key1 = nr_key2

After I have iterated through nr_key1 I want to use recursion to select nr_key1 = nr_dad1 and iterate again table2 with the new nr_key1. This should be repeated as long as nr_dad1 is null. I have succesfully got the result but only if nr_key1 = nr_key2 contains values. If table2 is empty for the current nr_key1 my recursion is broken. I receive error if I try to use outer join in my recursion. Also my current solution is quite slow, because I am iterating the whole hierarchy again from each value found in table2. Is it possible to implement some sort of a check?

Maybe my approach is wrong? Any suggestions will be highly appreciated thanks.

  1. WITH test (NR_KEY1, NR_DAD1, VALUE, leveli)
  2. AS
  3. (
  4. SELECT a.NR_KEY1, a.NR_DAD1, VALUE,
  5. 1 AS leveli
  6. FROM table1
  7. RIGHT OUTER JOIN table2 a ON
  8. NR_KEY2 = a.NR_KEY1 WHERE a.NR_KEY1 = 10020
  9.  
  10. union ALL
  11.  
  12. SELECT a.NR_KEY1, a.NR_DAD1, e.VALUE,
  13. eh.leveli + 1 AS leveli
  14. FROM table1 e
  15. INNER JOIN table2 a ON
  16. e.NR_KEY2 = a.NR_KEY1
  17. INNER JOIN test eh ON
  18. e.NR_KEY2 = eh.NR_DAD1
  19. WHERE e.NR_KEY2 = eh.NR_DAD1
  20. )
  21. SELECT * FROM test ORDER BY leveli
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 42
Reputation: huangzhi is an unknown quantity at this point 
Solved Threads: 13
huangzhi huangzhi is offline Offline
Light Poster
 
0
  #2
24 Days Ago
Can u give some data and the result you want?
or you can see http://www.daniweb.com/forums/thread114466.html.
Hence Wijaya
www.ex-Soft.tk
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 6
Reputation: elauri is an unknown quantity at this point 
Solved Threads: 0
elauri elauri is offline Offline
Newbie Poster
 
0
  #3
23 Days Ago
I shamelessly copied your code to make a example

  1. CREATE TABLE #tmpName (ID int, [Name] varchar(255))
  2. CREATE TABLE #tmpRelation (ID int, ID_Parent int)
  3.  
  4. INSERT #tmpName (ID, [Name]) select 1, 'value1'
  5. INSERT #tmpName (ID, [Name]) select 2, 'value2'
  6. INSERT #tmpName (ID, [Name]) select 2, 'value3'
  7. INSERT #tmpName (ID, [Name]) select 2, 'value4'
  8. INSERT #tmpName (ID, [Name]) select 4, 'value5'
  9.  
  10. INSERT #tmpRelation (ID, ID_Parent) select 1, 2
  11. INSERT #tmpRelation (ID, ID_Parent) select 2, 3
  12. INSERT #tmpRelation (ID, ID_Parent) select 3, 4
  13. INSERT #tmpRelation (ID, ID_Parent) select 4, NULL
  14. INSERT #tmpRelation (ID, ID_Parent) select 5, 1

I would like a following result:
nr;value;dad;level
1;value1;2;1
2;value2;3;2
2;value3;3;2
4;value4;null;4
Last edited by elauri; 23 Days Ago at 2:58 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 6
Reputation: elauri is an unknown quantity at this point 
Solved Threads: 0
elauri elauri is offline Offline
Newbie Poster
 
0
  #4
23 Days Ago
Sorry I forgot to mention the selection. For the above result I would select id 1 from the tmpRelation table and also there was a mistake in the result.

I would like a following result:
nr;value;dad;level
1;value1;2;1
2;value2;3;2
2;value3;3;2
2;value4;3;2
*3;null;4;3
4;value5;null;4

*(3 is not displayed because nothing is found, not problem if it has to be displayed with null value)
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 42
Reputation: huangzhi is an unknown quantity at this point 
Solved Threads: 13
huangzhi huangzhi is offline Offline
Light Poster
 
0
  #5
22 Days Ago
Try code below

  1. SET nocount ON
  2. CREATE TABLE #tmpName (ID int, [Name] varchar(255))
  3. CREATE TABLE #tmpRelation (ID int, ID_Parent int)
  4.  
  5. INSERT #tmpName (ID, [Name]) select 1, 'value1'
  6. INSERT #tmpName (ID, [Name]) select 2, 'value2'
  7. INSERT #tmpName (ID, [Name]) select 2, 'value3'
  8. INSERT #tmpName (ID, [Name]) select 2, 'value4'
  9. INSERT #tmpName (ID, [Name]) select 4, 'value5'
  10.  
  11. INSERT #tmpRelation (ID, ID_Parent) select 1, 2
  12. INSERT #tmpRelation (ID, ID_Parent) select 2, 3
  13. INSERT #tmpRelation (ID, ID_Parent) select 3, 4
  14. INSERT #tmpRelation (ID, ID_Parent) select 4, NULL
  15. INSERT #tmpRelation (ID, ID_Parent) select 5, 1
  16.  
  17. declare @ID_Start int, @Level int
  18. SELECT @Level = 0
  19. SELECT @ID_Start = 1
  20.  
  21. CREATE TABLE #tmpResult (ID int, ID_Parent int, [Level] int)
  22.  
  23. while NOT @ID_Start IS NULL
  24. begin
  25. SELECT @Level = @Level + 1
  26.  
  27. INSERT #tmpResult (ID, ID_Parent, [Level])
  28. SELECT R.ID, R.ID_Parent, @Level
  29. FROM #tmpRelation R
  30. WHERE ID = @ID_Start
  31.  
  32. SELECT @ID_Start = ID_Parent FROM #tmpRelation where ID = @ID_Start
  33. end
  34.  
  35. --> NR = 3 display
  36. SELECT R.ID AS NR, N.Name AS Value, ID_Parent AS Dad, [Level]
  37. FROM #tmpResult R left join #tmpName N on
  38. R.ID = N.ID
  39.  
  40. --> NR = 3 not display
  41. SELECT R.ID AS NR, N.Name AS Value, ID_Parent AS Dad, [Level]
  42. FROM #tmpResult R inner join #tmpName N on
  43. R.ID = N.ID
  44.  
  45. DROP TABLE #tmpResult
  46. DROP TABLE #tmpName
  47. DROP TABLE #tmpRelation
Hence Wijaya
www.ex-Soft.tk
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC