SQL recursive functions list all child levels of the parent level (ID parentid Mode)

Source: Internet
Author: User
  1. -- Call method:
  2. -- Select * From getchild ('24 ')
  3. -- Select ID from getchild ('24 ')
  4. -- Select * From kucun where producttype in (select ID from getchild ('24 '))
  5. Create Function [DBO]. [getchild] (@ ID varchar (10 ))
  6. Returns @ t table (ID varchar (10), parentid varchar (10), Level INT)
  7. As
  8. Begin
  9. Declare @ I int
  10. Set @ I = 1
  11. Insert into @ t select @ ID, @ ID, 0 -- current level, current level. If not, comment out or add another parameter to select an operation.
  12. Insert into @ t select ID, parentid, @ I from Dept where parentid = @ ID
  13. While @ rowcount <> 0
  14. Begin
  15. Set @ I = @ I + 1
  16. Insert into @ t
  17. Select
  18. A. ID, A. parentid, @ I
  19. From
  20. Dept a, @ T B
  21. Where
  22. A. parentid = B. ID and B. Level = @ I-1
  23. End
  24. Return
  25. End
  26. --------------------------------------------------------------------------------
  27. -- In SQL server2005, the CTE [Public table expression] is provided to implement recursion:
  28. Declare @ ID int
  29. Set @ ID = 24; --- modify the parent node here
  30. With rootnodecte (ID, parentid)
  31. As
  32. (
  33. Select ID, parentid from Dept where parentid in (@ ID)
  34. Union all
  35. Select Dept. ID, Dept. parentid from rootnodecte
  36. Inner join Dept
  37. On rootnodecte. ID = Dept. parentid
  38. )
  39. Select * From rootnodecte
  40. --------------------------------------------------------------------------------
  41. --------------------------------------------------------------------------------
  42. --------------------------------------------------------------------------------
  43. -- Generate Test Data
  44. Create Table dept (ID int, parentid int, MSG varchar (20 ))
  45. Insert into dept select 1, 0, null
  46. Insert into dept select 2, 1, null
  47. Insert into dept select 3, 1, null
  48. Insert into dept select 4, 2, null
  49. Insert into dept select 5, 3, null
  50. Insert into dept select 6, 5, null
  51. Insert into dept select 7,6, null
  52. Go
  53. -- Create a user-defined function
  54. Create Function [DBO]. [getchild] (@ ID varchar (10 ))
  55. Returns @ t table (ID varchar (10), parentid varchar (10), Level INT)
  56. As
  57. Begin
  58. Declare @ I int
  59. Set @ I = 1
  60. Insert into @ t select @ ID, @ ID, 0 -- current level, current level. If not, comment out or add another parameter to select an operation.
  61. Insert into @ t select ID, parentid, @ I from Dept where parentid = @ ID
  62. While @ rowcount <> 0
  63. Begin
  64. Set @ I = @ I + 1
  65. Insert into @ t
  66. Select
  67. A. ID, A. parentid, @ I
  68. From
  69. Dept a, @ T B
  70. Where
  71. A. parentid = B. ID and B. Level = @ I-1
  72. End
  73. Return
  74. End
  75. -- Execute Query
  76. Select ID from DBO. getchild (3)
  77. Go
  78. -- Output result
  79. /*
  80. 3
  81. 5
  82. 6
  83. 7
  84. */
  85. -- Delete test data
  86. Drop function getchild
  87. Drop table Dept
  88. Http://topic.csdn.net/u/20080409/16/1fb7d941-b1a1-4326-a936-230ddf057cbe.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.