The MATCH clause

SQL Server 2017 added the new MATCH clause to support traversing through graphs by specifying search conditions. Here is the syntax of the MATCH clause:

MATCH (<graph_search_pattern>)

<graph_search_pattern>::=
{<node_alias> {
{ <-( <edge_alias> )- }
| { -( <edge_alias> )-> }
<node_alias>
}
}
[ { AND } { ( <graph_search_pattern> ) } ]
[ ,...n ]

A typical query using the MATCH clause looks like this:

FROM node1, edge, node2
WHERE MATCH node1-(edge)->node2

You can see that the names of the involved tables are separated by a comma in the FROM clause, the same as in old-style JOIN statements. In the MATCH clause, you start from one node, write the edge table or alias in parentheses, and finally reach the other node table. To indicate the direction of the relation between the nodes, you can use a single dash (-), and dash and greater-than (->) characters. A single dash stays near the node table that is referenced in the $from_id edge column, and a dash and greater-than character represents the relation with the $to_id edge column. You can also use an arrow pointing in the other direction to point from node2 to node1:

FROM node1, edge, node2
WHERE MATCH node1-(edge)->node2

You can also use aliases for table names (sometimes, you must) as in the following example:

FROM node1 n1, edge 2, node2 n2
WHERE MATCH n1-(e)->n2

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. This part of the query is correct:

FROM node1, edge1, node2, edge2, node3 
WHERE MATCH node1-(edge1)->node2 AND MATCH node1<-(edge2)-node3

However, an edge table name cannot be repeated inside the MATCH clause, and this pseudo-code won't work:

FROM node1, edge1, node2, node3 
WHERE MATCH node1-(edge1)->node2 AND MATCH node1<-(edge1)-node3

In this case, you need to create another instance of the edge table:

FROM node1, edge1 AS e1, node2, node3, edge1 AS e2 
WHERE MATCH node1-(e1)->node2 AND MATCH node1<-(e2)-node3

You saw that the MATCH clause can be combined with other expressions. However, only the AND operator is supported; you cannot use OR and NOT operators in the WHERE clause when you use MATCH.

This will be clear when you see the MATCH clause in action against the graph tables created and populated in the next section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.24.106