944,193 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Marked Solved
  • Views: 1309
  • MS SQL RSS
Nov 8th, 2009
0

MS SQL Recursion problem

Expand Post »
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.

MS SQL Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
elauri is offline Offline
6 posts
since Nov 2009
Nov 9th, 2009
0
Re: MS SQL Recursion problem
Can u give some data and the result you want?
or you can see http://www.daniweb.com/forums/thread114466.html.
Reputation Points: 10
Solved Threads: 13
Light Poster
huangzhi is offline Offline
48 posts
since Feb 2008
Nov 10th, 2009
0
Re: MS SQL Recursion problem
I shamelessly copied your code to make a example

MS SQL Syntax (Toggle Plain Text)
  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; Nov 10th, 2009 at 2:58 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
elauri is offline Offline
6 posts
since Nov 2009
Nov 10th, 2009
0
Re: MS SQL Recursion problem
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)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
elauri is offline Offline
6 posts
since Nov 2009
Nov 10th, 2009
0
Re: MS SQL Recursion problem
Try code below

MS SQL Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 13
Light Poster
huangzhi is offline Offline
48 posts
since Feb 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MS SQL Forum Timeline: Connect by prior command
Next Thread in MS SQL Forum Timeline: SSIS skip bad row





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC