SQL Server series: Common table Expression CTE

Source: Internet
Author: User

1 CTE syntax with keyword

A common table expression (Common table Express, CTE) that defines the derived table at the top of the query. To start creating a query using a CTE, you can use the WITH keyword.

  CTE Syntax:

 with <expression_name>[(column_name [,... n])]    as      (cte_query_definition)    [, <another_expression>]  <query>

The CTE is first provided with a name similar to the alias used to derive the table. You can then provide the list of column names that the CTE will return, which is optional if the CTE specifies all of its returned columns. Finally, add the definition of the CTE query in parentheses, and finally add the main query that uses the CTE.

  The statement preceding the WITH keyword must use a semicolon (;) end.

2 Using multiple CTE

3 Recursive CTE

SQL Server series: Common table Expression CTE

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.