Press "Enter" to skip to content

Shortest Path with T-SQL Graph

Niko Neugebauer shows us how to use the SHORTEST_PATH() function with graph tables in SQL Server 2019:

SHORTEST_PATH() function will allow you to traverse the given graph looking for the shortest path between different Nodes. It will use the Arbitrary Length Pattern to define the traversal path. This function will not return any results any results in SELECT clause because it must be used within MATCH clause only!

To my understanding because one of the mechanisms being used is depth-first search, in situation where multiple shortest path do exist, the function will return the first one only.

Click through for a detailed article on the topic. There are some nice parts to this but also a couple not-so-nice limitations in the current CTP.