SQL Server's CTE implementation recursive query

Source: Internet
Author: User

--recursive queryIF object_id('Digui','U') is  not NULL     DROP TABLEDiguiCREATE TABLEDigui (IdVARCHAR( -), ParentIDVARCHAR( -))INSERT  intodbo. Digui (Id, ParentID)Select 'Third Floor','First Floor'Union Select 'Second Floor','First Floor'Union Select 'Fourth Floor','First Floor'Union Select 'Tenth Floor','Second Floor'Union Select 'Nineth Floor','Second Floor'Union Select 'Eighth Floor','Fifth Floor'Union Select '20th Floor','Fourth Floor'Union Select '22nd Floor','Fourth Floor'Union Select '30th Floor','20th Floor'Union Select '32nd Floor','20th Floor'Union Select '40th Floor','30th Floor'Union Select '42nd Floor','30th Floor'Union Select '50th Floor','40th Floor'Union Select '52nd Floor','40th Floor'Union Select '80th Floor','50th Floor'Union Select '82nd Floor','50th Floor'Union Select '100th Floor','90th Floor'Union Select '102th Floor','90th Floor'; with Temp as(SelectId, ParentID fromDiguiwhere [ParentID] = 'First Floor'Union  AllSelecta.id, A.parentid fromDigui aInner Join Temp  onA.[ParentID] = Temp.[Id])Select *  from Temp

SQL Server's CTE implementation recursive query

Related Article

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.