T-SQL recursive query

Source: Internet
Author: User

Querying a recursive hierarchy using a CTE: (SQL CTE recursion)

1  withCte_ma as 2 (3     SELECT 0  asTier, EmployeeID, ManagerID, FirstName, LastName4      fromdbo. MyEmployees5     WHEREEmployeeID= 16     UNION  All7     SELECTA.tier+1  asTier, E.employeeid, E.managerid, E.firstname, E.lastname8      fromdbo. MyEmployees e9     INNER JOINCte_ma A onE.managerid=A.employeeidTen ) One SELECT *  fromCte_ma

Tier        EmployeeID managerid   FirstName                      LastName------------------------------------------------------- -----------------------------------------------0           1          NULL        Ken                            sánchez1           273        1           Brian                          Welcker2         273         David                          Bradley2           274        273         Stephen                        Jiang2           285        273         Syed                           Abbas3           286        285         Lynn                           Tsoflias3           275        274         Michael                        Blythe3           276        274         Linda                          Mitchell3           ,          Mary                           Gibson

T-SQL recursive query

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.