SQL Server (starting with SQL Server 2017) and Azure SQL Database now let you create a graph database, to hold your entities and complex many to many relationships. There are several examples on github which demonstrate how the new graph features work. This example shows how you can use SHORTEST_PATH function to write a shortest path or arbitrary length traversal query. This feature is available for public preview with SQL Server 2019 CTP3.1
To demonstrate the functionality, we will be using AdventureWorks2014 as our sample database.
About this sample
Before you begin
Run this sample
Related links
- Applies to:
- SQL Server 2019 CTP3.1 (or higher)
- Demos:
- Build and populate graph node and edge tables
- Use SHORTEST_PATH function to compute shortest path
- between 2 given nodes.
- starting from a given node to all other nodes in the graph.
- starting from a given node to all other nodes, which are 1 to 3 hops away from the start node.
- Workload: Queries executed on AdventureWorks2014
- Programming Language: T-SQL
- Author: Shreya Verma
To run these demo scripts, you need the following prerequisites.
Account and Software prerequisites:
- SQL Server 2019 CTP3.1 (or higher)
- SQL Server Management Studio 18.x (or higher)
-
Download AdventureWorks2014.bak
-
Launch SQL Server Management Studio, connect to your SQL Server instance (2019) and restore AdventureWorks2014.bak. This document describes how to Restore a Database Backup Using SSMS.
- Once the database is restored, run the setup-BOMGraph.sql script to create the necessary graph node and edge tables. We will be using these tables to run our sample queries. The setup file creates Product node table and IsPartOf edge table, which represents the BOM for products in AdventureWorks Cycles manufacturing pipeline.
- Run the ShortestPath.sql script to run some SHORTEST_PATH queries. The script has following example queries:
- Starting from a given node, find shortest path to all the other nodes in the graph.
- Starting from a given node, find shortest path to all the other nodes in the graph, which are 1 -3 hops away from the start node.
- Find shortest path between 2 given nodes (start and end node)
For more information about Graph DB in SQL Server 2017, see these articles:
