Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. retrive child and sub child from table [modified]

retrive child and sub child from table [modified]

Scheduled Pinned Locked Moved ASP.NET
csharpasp-netdatabasetutorialquestion
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Punit Belani
    wrote on last edited by
    #1

    sorry buddys it might be in wrong category, but i use ASP.NET platform for devloping so... now my question: i have table formated as three column: id,parent_id,name ex.. id /parent_id /name 1 /null /ABC1 2 /1 /ABC2 3 /1 /ABC3 4 /2 /ABC4 5 /4 /ABC5 6 /3 /ABC6 for example i wnt to retrive perticulars child and sub child of id 2 then it should give result: 2 /1 /ABC2 4 /2 /ABC4 5 /4 /ABC5 actually i want perticulars child and sub childs.. if id is 2 den i want child of id 2, and sub childs of id 2,s child and so on.. Please giv me appropriate query for it... thanks in advance

    modified on Wednesday, August 19, 2009 2:58 AM

    B P N A 4 Replies Last reply
    0
    • P Punit Belani

      sorry buddys it might be in wrong category, but i use ASP.NET platform for devloping so... now my question: i have table formated as three column: id,parent_id,name ex.. id /parent_id /name 1 /null /ABC1 2 /1 /ABC2 3 /1 /ABC3 4 /2 /ABC4 5 /4 /ABC5 6 /3 /ABC6 for example i wnt to retrive perticulars child and sub child of id 2 then it should give result: 2 /1 /ABC2 4 /2 /ABC4 5 /4 /ABC5 actually i want perticulars child and sub childs.. if id is 2 den i want child of id 2, and sub childs of id 2,s child and so on.. Please giv me appropriate query for it... thanks in advance

      modified on Wednesday, August 19, 2009 2:58 AM

      B Offline
      B Offline
      Blikkies
      wrote on last edited by
      #2

      Can you expl more because what you asking and what your result is doesnt make sence.

      P 1 Reply Last reply
      0
      • P Punit Belani

        sorry buddys it might be in wrong category, but i use ASP.NET platform for devloping so... now my question: i have table formated as three column: id,parent_id,name ex.. id /parent_id /name 1 /null /ABC1 2 /1 /ABC2 3 /1 /ABC3 4 /2 /ABC4 5 /4 /ABC5 6 /3 /ABC6 for example i wnt to retrive perticulars child and sub child of id 2 then it should give result: 2 /1 /ABC2 4 /2 /ABC4 5 /4 /ABC5 actually i want perticulars child and sub childs.. if id is 2 den i want child of id 2, and sub childs of id 2,s child and so on.. Please giv me appropriate query for it... thanks in advance

        modified on Wednesday, August 19, 2009 2:58 AM

        P Offline
        P Offline
        padmanabhan N
        wrote on last edited by
        #3

        sorry, this is wrong forum... if you post you question in a appropriate forum you will be clearly guided by our experts.....

        Padmanabhan My Articles: Articles[^] My latest Article: Word Automation[^]

        1 Reply Last reply
        0
        • B Blikkies

          Can you expl more because what you asking and what your result is doesnt make sence.

          P Offline
          P Offline
          Punit Belani
          wrote on last edited by
          #4

          actually i want perticulars child and sub childs.. if id is 2 den i want child of id 2, and sub childs of id 2,s child and so on..

          1 Reply Last reply
          0
          • P Punit Belani

            sorry buddys it might be in wrong category, but i use ASP.NET platform for devloping so... now my question: i have table formated as three column: id,parent_id,name ex.. id /parent_id /name 1 /null /ABC1 2 /1 /ABC2 3 /1 /ABC3 4 /2 /ABC4 5 /4 /ABC5 6 /3 /ABC6 for example i wnt to retrive perticulars child and sub child of id 2 then it should give result: 2 /1 /ABC2 4 /2 /ABC4 5 /4 /ABC5 actually i want perticulars child and sub childs.. if id is 2 den i want child of id 2, and sub childs of id 2,s child and so on.. Please giv me appropriate query for it... thanks in advance

            modified on Wednesday, August 19, 2009 2:58 AM

            N Offline
            N Offline
            N a v a n e e t h
            wrote on last edited by
            #5

            Since you asked about query, here is a possible implementation using stored procedure. 1 - Write a stored procedure that takes a parent id. 2 - Do a select to get all the children of the supplied parent id and assign it to a T-SQL cursor. 3 - Iterate over all of the children. 3.1 - Add row into a temporary table 3.2 - Call this procedure again (recursive) by passing the child id as parent id. Goes to step 2 4 - Select all the rows from the temporary table if @@NESTLEVEL is 1. 5 - Drop temporary table, de-allocate cursor. Note: SQL server has a stored procedure nesting limit. I guess it is 32. So if you have an item which is nested more than 32 levels, you will get error. In such case, you need to handle this in the application code rather than on a procedure. Also you need to make sure that the temporary table is getting created only once, that is when the procedure starts executing for the first time. All the nested calls will get the temporary table created on the first call. This can be done in a better way using C# (or whatever language you use).

            Navaneeth How to use google | Ask smart questions

            P 1 Reply Last reply
            0
            • P Punit Belani

              sorry buddys it might be in wrong category, but i use ASP.NET platform for devloping so... now my question: i have table formated as three column: id,parent_id,name ex.. id /parent_id /name 1 /null /ABC1 2 /1 /ABC2 3 /1 /ABC3 4 /2 /ABC4 5 /4 /ABC5 6 /3 /ABC6 for example i wnt to retrive perticulars child and sub child of id 2 then it should give result: 2 /1 /ABC2 4 /2 /ABC4 5 /4 /ABC5 actually i want perticulars child and sub childs.. if id is 2 den i want child of id 2, and sub childs of id 2,s child and so on.. Please giv me appropriate query for it... thanks in advance

              modified on Wednesday, August 19, 2009 2:58 AM

              A Offline
              A Offline
              Abhishek Sur
              wrote on last edited by
              #6

              you mean you need hierarchical data. If particular node is chosen, you need all its child elements. I think it is very easy to do in Recursion, but very tough to do it in a single SQL. Try it, It needs Self joins in query. I will do this when get time and let you know. :thumbsup:

              Abhishek Sur


              My Latest Articles **Create CLR objects in SQL Server 2005 C# Uncommon Keywords Read/Write Excel using OleDB

              **Don't forget to click "Good Answer" if you like to.

              P 1 Reply Last reply
              0
              • A Abhishek Sur

                you mean you need hierarchical data. If particular node is chosen, you need all its child elements. I think it is very easy to do in Recursion, but very tough to do it in a single SQL. Try it, It needs Self joins in query. I will do this when get time and let you know. :thumbsup:

                Abhishek Sur


                My Latest Articles **Create CLR objects in SQL Server 2005 C# Uncommon Keywords Read/Write Excel using OleDB

                **Don't forget to click "Good Answer" if you like to.

                P Offline
                P Offline
                Punit Belani
                wrote on last edited by
                #7

                ok.. waitin for ur reply.. thanks..

                1 Reply Last reply
                0
                • N N a v a n e e t h

                  Since you asked about query, here is a possible implementation using stored procedure. 1 - Write a stored procedure that takes a parent id. 2 - Do a select to get all the children of the supplied parent id and assign it to a T-SQL cursor. 3 - Iterate over all of the children. 3.1 - Add row into a temporary table 3.2 - Call this procedure again (recursive) by passing the child id as parent id. Goes to step 2 4 - Select all the rows from the temporary table if @@NESTLEVEL is 1. 5 - Drop temporary table, de-allocate cursor. Note: SQL server has a stored procedure nesting limit. I guess it is 32. So if you have an item which is nested more than 32 levels, you will get error. In such case, you need to handle this in the application code rather than on a procedure. Also you need to make sure that the temporary table is getting created only once, that is when the procedure starts executing for the first time. All the nested calls will get the temporary table created on the first call. This can be done in a better way using C# (or whatever language you use).

                  Navaneeth How to use google | Ask smart questions

                  P Offline
                  P Offline
                  Punit Belani
                  wrote on last edited by
                  #8

                  ok... i understand ur method, but i faced problem to create procedure.. can you reply me demo procedure??? thanks..

                  N 1 Reply Last reply
                  0
                  • P Punit Belani

                    ok... i understand ur method, but i faced problem to create procedure.. can you reply me demo procedure??? thanks..

                    N Offline
                    N Offline
                    N a v a n e e t h
                    wrote on last edited by
                    #9

                    punit_belani wrote:

                    but i faced problem to create procedure

                    What problem are you facing in creating a procedure?

                    punit_belani wrote:

                    can you reply me demo procedure

                    Its very easy to create your own procedure with the steps provided in my last post. If you have problems, come back with specific error messages. Do a search on creating nested stored procedures and learn to do it yourself. Spoon feeding will not benefit you.

                    Navaneeth How to use google | Ask smart questions

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups