Home > database >  SQL Server using graph table how to write a recursive query
SQL Server using graph table how to write a recursive query

Time:11-11

SQL from 2017 began to support graph table, want to ask how to write a recursive query?

Instances are as follows:
The CREATE TABLE Person (
ID is an INTEGER PRIMARY KEY,
The name VARCHAR (100)
);

The CREATE TABLE friendOf AS EDGE;

INSERT INTO Person (Id, name)
VALUES (1, 'John')
, (2, 'Mary')
, (3, 'Alice')
, (4, 'Jacob')
, (5, "Julie");

INSERT INTO friendOf
VALUES ($NODE_ID (SELECT FROM the Person WHERE ID=1), (SELECT $NODE_ID FROM Person WHERE ID=2))
, ($NODE_ID (SELECT FROM the Person WHERE ID=2), (SELECT $NODE_ID FROM Person WHERE ID=3))
, ($NODE_ID (SELECT FROM the Person WHERE ID=3), (SELECT $NODE_ID FROM Person WHERE ID=1))
, ($NODE_ID (SELECT FROM the Person WHERE ID=4), (SELECT $NODE_ID FROM Person WHERE ID=2))
, ($NODE_ID (SELECT FROM the Person WHERE ID=5), (SELECT $NODE_ID FROM Person WHERE ID=4));

Query 'John' friend:

Select p1. Name, p2. Name
The from Person p1, friendOf f, Person p2
Where the match (p1 - (f) - & gt; P2)
And p1. Name='John'

But if I want to check all of the 'John' friends, friends of friends,...
How to write the SQL?

If it's not graph table, is to be able to write a recursive query.

thank you
  • Related