I feel this CTE recursive query is very good, let me give an example:
[C-sharp]View Plain copy print?
- Use city ;
- Go
- CREATE TABLE Tree
- (
- ID int identity (primary) key is not null,
- Name varchar () is not null,
- Parent varchar () null
- )
- Go
- Insert Tree VALUES (' university ',null)
- Insert Tree VALUES (' College ','University ')
- Insert Tree VALUES (' Computer Academy',' College ')
- Insert Tree VALUES (' network Engineering ',' Computer Academy ')
- Insert Tree VALUES (' information management ',' Computer Academy ')
- Insert Tree VALUES (' Telecom Academy',' College ')
- Insert Tree VALUES (' academic Office ',' university ')
- Insert Tree VALUES (' materials section ',' Dean's Office ')
- Insert Tree VALUES (' admissions Office ',' university ')
- Go
- With CTE as
- (
- -->begin A locator point member
- Select ID, Name,parent,cast (Name as nvarchar (max)) as te,0 as Levle from Tree where the Parent is null
- -->end
- Union All
- -->begin a recursive member
- Select Tree.id, Tree.name,tree.parent,cast (Replicate (", Len (cte.te)) +' |_ ' +tree.name as nvarchar (MAX)) as te,levle+1 as Levle
- From Tree INNER join CTE
- On tree.parent=cte. Name
- -->end
- )
- SELECT * from the CTE order by ID
- --1. Splitting a CTE expression into an anchor member and a recursive member.
- --2. Run an anchor member to create the first call or datum result set (T0).
- --3. Run recursive members, use Ti as input (there is only one record here), and ti+1 as output.
- --4. Repeat step 3 until the empty set is returned.
- --5. Returns the result set. This is the result of the T0 to Tn execution of UNION all.
The above SQL statement inserts a single piece of data again:
Insert Tree VALUES (' Network 1 class ', ' Network Engineering ')
Running results such as:
Figure 1 Running results
Note: It seems that the field selected at the recursive member must be the data of the tree table, not the one in the CTE result set except the tree, and the fields in the CTE can be referenced here, such as the field Te.
First look at the SQL statement that iterates through the 1th record:
[C-sharp]View Plain copy print?
- Select ID, Name,parent,cast (Name as nvarchar (max)) as te,0 as Levle from Tree where the Parent is null
The results obtained are:
Name Parent TE Levle
-------------------------------------
University of NULL University 0
The class SQL statement for the result collection that is obtained recursively for the 2nd time is:
[C-sharp]View Plain copy print?
- select tree.id, tree.name,tree.parent,cast (Replicate ( ' , Len (cte.te)) + ' |_ ' +tree.name as nvarchar (MAX)) as te,levle+1 as levle
- from tree inner join
- (Select id, name,parent,cast (name as nvarchar (max)) as te,0 as levle from tree where parent is null )
- as cte
- on Tree.parent=cte. name
The result of the above CTE subquery is the result set of the first recursive query, the SQL run result is:
Similarly, the above three records of the second recursive query are the ' locating members ' of the third query:
"Note here that the above three records are the input of the third recursive, starting with the last one, that is, the first is the record of the Id=9, followed by 7 and 2, and the fourth recursion is similar"
Third recursive collation SQL statement
[C-sharp]View Plain copy print?
- Select Tree.id, Tree.name,tree.parent,cast (Replicate (", Len (cte.te)) +' |_ ' +tree.name as nvarchar (MAX)) as te,levle+1 as Levle
- From Tree INNER join
- (SQL statement for second recursive query) as CTE
- On tree.parent=cte. Name
The results are as follows:
In fact, each recursive class of SQL can be shown as follows:
[C-sharp]View Plain copy print?
- Select Tree.id, Tree.name,tree.parent,cast (Replicate (", Len (cte.te)) +' |_ ' +tree.name as nvarchar (MAX)) as te,levle+1 as Levle
- From Tree INNER join
- (The result set of the last recursive query, just the last time, not the previous sum result set)
- As CTE
- On tree.parent=cte. Name
The fourth time recursion and so on, and the result of the last query is the union of all the recursion above.
Continuation: In the above sq statement query results, the record with ID 10 should be placed after ID 4.
Add two records to the datasheet again:
Insert Tree VALUES (' Accounting section ', ' Computer Academy ')
Insert Tree values (' I ', ' Network 1 classes ')
Modify the above SQL statement again:
[C-sharp]View Plain copy print?
- With CTE as
- (
- -->begin A locator point member
- Select ID, Name,parent,cast (Name as nvarchar (max)) as TE,
- row_number () over (order by GETDATE ()) as OrderID
- -The most critical is the above field, to get the sort field, sorted by string.
- --where the window function must use order BY, but not the integer type, then use the time.
- From Tree where the Parent is null
- -->end
- Union All
- -->begin a recursive member
- Select Tree.id, Tree.name,tree.parent,cast (Replicate (", Len (cte.te)) +' |_ '+ Tree.name as nvarchar (MAX)) as TE,
- CTE. Orderid*100+row_number () over (Order by GETDATE ()) as OrderID
- From Tree INNER join CTE
- On tree.parent=cte. Name
- -->end
- )
- SELECT * from CTE
- ORDER by LTRIM (OrderID)--to sort this integer data into a string type
-
- --Sometimes the integer can be larger than the size, the string is also possible, and the size of the string is a character comparison.
- --int + string = = Integer, only string + string = = two string and and
- --Recursive query: The second record can refer to the value of the first record
- --record identifier of the same level when dynamically loading records: RowNumber () over (order by GETDATE ())
- --Extension: You can dynamically get the sub-department under a department. You can also get the department's superior
-
- --Summary: First to piece together an integer data, then converted to a string, and finally the order of the string, not the integer data order,
The final result is:
Figure 2 Running results
This way, no matter how many records a user inserts, they can be queried by department and by law.
The CTE recursive query in SQL Server 2005 gets a tree