Tham khảo tài liệu 'expert sql server 2008 development- p9', công nghệ thông tin, cơ sở dữ liệu phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | CHAPTER 12 TREES HIERARCHIES AND GRAPHS AS SELECT @Start AS theStart IntersectionId_End AS theEnd FROM WHERE IntersectionId_Start @Start UNION ALL SELECT FROM Paths p JOIN ss ON WHERE @End SELECT FROM Paths GO The anchor part of the CTE finds all nodes to which the starting intersection is connected in this case given the data we ve already input there is only one. The recursive part uses the anchor s output as its input finding all connected nodes from there and continuing only if the endpoint of the next intersection is not equal to the end intersection. The output for this query is as follows theStart theEnd 1 2 2 3 3 4 While this output is correct and perfectly descriptive with only one path between the two points it has some problems. First of all the ordering of the output of a CTE just like any other query is not guaranteed without an ORDER BY clause. In this case the order happens to coincide with the order of the path but this is a very small data set and the server on which I ran the query has only one processor. On a bigger set of data and or with multiple processors SQL Server could choose to process the data in a different order thereby destroying the implicit output order. The second issue is that in this case there is exactly one path between the start and endpoints. What if there were more than one path Figure 12-6 shows the street map with a new street a few new intersections and more street segments added. The following T-SQL can be used to add the new data to the appropriate tables 381 CHAPTER 12 TREES HIERARCHIES AND GRAPHS --New street INSERT INTO Streets VALUES 6 Lexington GO --New intersections INSERT INTO Intersections VALUES 5 E 6 F 7 G 8 H GO --New intersection street mappings INSERT INTO IntersectionStreets VALUES 5 1 5 6 6 2 6 6 7 3 7 6 8 4 8 6 GO --North South segments INSERT INTO StreetSegments VALUES 2 6 2 4 8 4 GO --East .