Traversing hierarchy data in T-SQL using queues

Source: Internet
Author: User
Tags unique id sql using

A few days ago to see the "the Guru's Guide to Transact SQL" (Chinese translation is a Transact-SQL authoritative guide), which describes a query is about the hierarchy, the way to deal with the nature of the queue, traversal of the result is like traversing the binary tree in the first sequence traversal, very interesting.

The table structure to be queried is as follows:

Each layer node assigns a unique ID, the name of the node, and the ID of the previous parent node. Now, to find all the child nodes under the specified node ID, for example, for a node with ID 3, its query result ID set is {5,6,7,8,9,10,11,12,13}. Here is the key, write a stored procedure for easy reuse.

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.