SQL Server 2005 uses a relational database to store the CFG

Source: Internet
Author: User
Tags execution sql query
This article describes information that uses a relational database to store the CFG (Control flowchart). In the query, a CTE is used for recursive queries in order to obtain the results of all dominate relationships.

This article describes information that uses a relational database to store the CFG (Control flowchart). In the query, a CTE is used for recursive queries in order to obtain the results of all dominate relationships.

The simple example is as follows:

1. The existing table Testre structure is as follows

2. The result of the query is to find all the PID of the item of id= ' A ', namely B, C, D.

3. Use a CTE to do a recursive query:

With Re_cte (IDs, PID, level)
Technorati Tags: sql server,cte, recursive, query as 
( 
    SELECT  ID, PID, 1 as [level]
  from  testre 
    WHERE id= ' A '  
    UNION all 
    SELECT  t.id, T.pid, [level] + 1 
    from  testre t  INNER JOIN Re_cte CT 
    On T.id=ct.pid 
) 
SELECT * from Re_cte

Where the level is optional

4. The results of the query are:

5. A simple description of the CTE (reference msdnhttp://msdn.microsoft.com/zh-cn/library/ms186243%28sql.90%29.aspx):

With Cte_name (column_name [,... n]) as cte_query_definition–-the Anchor member is 
defined. 
UNION all 
cte_query_definition–-recursive the defined referencing Cte_name. 
) 
--Statement using the CTE 
SELECT * from 
cte_name

A recursive CTE structure must contain at least one anchor member and one recursive member.

At the time of execution:

Splits a CTE expression into an anchor member and a recursive member.

Run the anchor member to create the first call or datum result set (T0).

Run the recursive member, the Ti as input, the ti+1 as output.

Repeat step 3 until the empty set is returned.

Returns the result set. This is the result of the execution of UNION all for T0 to Tn.



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.