You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
| ( {<node_first_al_pattern> [<node_first_al_pattern> ... , n ] )
81
+
<al_pattern_quantifier>
82
+
LAST_NODE( <node_alias> ) | <node_alias>
82
83
}
83
-
84
+
84
85
<edge_first_al_pattern> ::=
85
-
{ (
86
-
{ -( <edge_alias> )-> }
87
-
| { <-( <edge_alias> )- }
86
+
{ (
87
+
{ -( <edge_alias> )-> }
88
+
| { <-( <edge_alias> )- }
88
89
<node_alias>
89
-
)
90
-
}
90
+
)
91
+
}
91
92
92
93
<node_first_al_pattern> ::=
93
-
{ (
94
-
<node_alias>
95
-
{ <-( <edge_alias> )- }
94
+
{ (
95
+
<node_alias>
96
+
{ <-( <edge_alias> )- }
96
97
| { -( <edge_alias> )-> }
97
-
)
98
-
}
99
-
98
+
)
99
+
}
100
100
101
101
<al_pattern_quantifier> ::=
102
102
{
103
-
+
104
-
| { 1 , n }
103
+
+
104
+
| { 1 , n }
105
105
}
106
106
107
107
n - positive integer only.
108
-
109
108
```
110
109
111
110
## Arguments
112
-
*graph_search_pattern*
111
+
112
+
#### *graph_search_pattern*
113
+
113
114
Specifies the pattern to search or path to traverse in the graph. This pattern uses ASCII art syntax to traverse a path in the graph. The pattern goes from one node to another via an edge, in the direction of the arrow provided. Edge names or aliases are provided inside parentheses. Node names or aliases appear at the two ends of the arrow. The arrow can go in either direction in the pattern.
114
115
115
-
*node_alias*
116
+
#### *node_alias*
117
+
116
118
Name or alias of a node table provided in the FROM clause.
117
119
118
-
*edge_alias*
120
+
#### *edge_alias*
121
+
119
122
Name or alias of an edge table provided in the FROM clause.
120
123
121
-
*SHORTEST_PATH*
124
+
#### *SHORTEST_PATH*
125
+
122
126
Shortest path function is used to find shortest path between two given nodes in a graph or between a given node and all the other nodes in a graph. It takes an arbitrary length pattern as input, that is searched repeatedly in a graph. Introduced in SQL Server 2019. Requires SQL Server 2019 or later.
123
127
124
-
*arbitrary_length_match_pattern*
125
-
Specifies the nodes and edges that must be traversed repeatedly until the desired node is reached or until the maximum number of iterations as specified in the pattern is met.
128
+
#### *arbitrary_length_match_pattern*
129
+
130
+
Specifies the nodes and edges that must be traversed repeatedly until the desired node is reached or until the maximum number of iterations as specified in the pattern is met.
131
+
132
+
#### *al_pattern_quantifier*
133
+
134
+
The arbitrary length pattern takes regular expression style pattern quantifiers in order to specify the number of times a given search pattern is repeated. The supported search pattern quantifiers are:
135
+
136
+
-**+**: Repeat the pattern 1 or more times. Terminate as soon as a shortest path is found.
137
+
-**{1,n}**: Repeat the pattern 1 to *n* times. Terminate as soon as a shortest path is found.
126
138
127
-
*al_pattern_quantifier*
128
-
The arbitrary length pattern takes regular expression style pattern quantifiers in order to specify the number of times a given search pattern is repeated. The supported search pattern quantifiers are:
129
-
***+**: Repeat the pattern 1 or more times. Terminate as soon as a shortest path is found.
130
-
***{1,n}**: Repeat the pattern 1 to *n* times. Terminate as soon as a shortest path is found.
139
+
## Remarks
131
140
132
-
## Remarks
133
-
The node names inside MATCH can be repeated. In other words, a node can be traversed an arbitrary number of times in the same query.
134
-
An edge name cannot be repeated inside MATCH.
141
+
The node names inside MATCH can be repeated. In other words, a node can be traversed an arbitrary number of times in the same query.
142
+
An edge name can't be repeated inside MATCH.
135
143
An edge can point in either direction, but it must have an explicit direction.
136
-
OR and NOT operators are not supported in the MATCH pattern.
137
-
MATCH can be combined with other expressions using AND in the WHERE clause. However, combining it with other expressions using OR or NOT is not supported.
144
+
OR and NOT operators aren't supported in the MATCH pattern.
145
+
MATCH can be combined with other expressions using AND in the WHERE clause. However, combining it with other expressions using OR or NOT isn't supported.
138
146
139
-
## Examples
140
-
### A. Find a friend
141
-
The following example creates a Person node table and friends Edge table, inserts some data and then uses MATCH to find friends of Alice, a person in the graph.
147
+
## Examples
142
148
143
-
```sql
149
+
### Find a friend
150
+
151
+
The following example creates a Person node table and friends Edge table, inserts some data and then uses MATCH to find friends of Alice, a person in the graph.
152
+
153
+
```sql
144
154
-- Create person node table
145
155
CREATETABLEdbo.Person (ID INTEGERPRIMARY KEY, name VARCHAR(50)) AS NODE;
146
156
CREATETABLEdbo.friend (start_date DATE) AS EDGE;
@@ -167,48 +177,66 @@ WHERE MATCH(Person1-(friend)->Person2)
167
177
ANDPerson1.name='Alice';
168
178
```
169
179
170
-
### B. Find friend of a friend
171
-
The following example tries to find friend of a friend of Alice.
180
+
### Find friend of a friend
172
181
173
-
```sql
174
-
SELECTPerson3.nameAS FriendName
182
+
The following example tries to find friend of a friend of Alice.
183
+
184
+
```sql
185
+
SELECTPerson3.nameAS FriendName
175
186
FROM Person Person1, friend, Person Person2, friend friend2, Person Person3
176
187
WHERE MATCH(Person1-(friend)->Person2-(friend2)->Person3)
177
188
ANDPerson1.name='Alice';
178
189
```
179
190
180
-
### C. More `MATCH` patterns
181
-
Following are some more ways in which a pattern can be specified inside MATCH.
191
+
### Find people 1-3 hops away from a given person
192
+
193
+
The following example finds the shortest path between Jacob and all the people that Jacob is connected to in the graph one to three hops away from him.
182
194
183
-
```sql
195
+
```sql
196
+
SELECT
197
+
Person1.nameAS PersonName,
198
+
STRING_AGG(Person2.name, '->') WITHIN GROUP (GRAPH PATH) AS Friends
199
+
FROM
200
+
Person AS Person1,
201
+
friendOf FOR PATHAS fo,
202
+
Person FOR PATHAS Person2
203
+
WHERE MATCH(SHORTEST_PATH(Person1(-(fo)->Person2){1,3}))
204
+
ANDPerson1.name='Jacob'
205
+
```
206
+
207
+
### More patterns
208
+
209
+
Following are some more ways in which a pattern can be specified inside MATCH.
210
+
211
+
```sql
184
212
-- Find a friend
185
213
SELECTPerson2.nameAS FriendName
186
214
FROM Person Person1, friend, Person Person2
187
215
WHERE MATCH(Person1-(friend)->Person2);
188
-
216
+
189
217
-- The pattern can also be expressed as below
190
218
191
219
SELECTPerson2.nameAS FriendName
192
-
FROM Person Person1, friend, Person Person2
220
+
FROM Person Person1, friend, Person Person2
193
221
WHERE MATCH(Person2<-(friend)-Person1);
194
222
195
-
196
223
-- Find 2 people who are both friends with same person
0 commit comments