递归关系查询

时间:2018-05-18 14:00:39

标签: azure azure-sql-database

我希望实现图表来映射Azure SQL中我的应用程序的角色层次结构。如果布局,图表看起来就像一棵树。父母可以管理树下任何角色。

所以我有一个角色节点表和一个canmanage边缘表。

我熟悉查询第一级和第二级关系,但是我需要有一个查询,我可以在其中放置任何角色并接收属于它的所有子级的列表。

我对NEO4J中的这类事情很熟悉,但我没有找到任何关于如何在Azure SQL中完成此任务的文档。

如何运行递归查询以使所有子角色都给出特定的角色名称或ID?

1 个答案:

答案 0 :(得分:1)

这可以从SQL Server 2017和Azure SQL DB使用new graph database capabilities和新的MATCH子句来建模这种类型的关系。遗憾的是,在v1中,多态性和传递闭包不是本机包含的,但可以使用递归查询。如果查看最后一个查询,它会将您输入的参数保留为顶级管理器,并迭代其余查询。

示例脚本:

USE tempdb
GO

-- NODES
DROP TABLE IF EXISTS dbo.roles

-- EDGES
DROP TABLE IF EXISTS dbo.canManage
DROP TABLE IF EXISTS dbo.isManagedBy
GO

CREATE TABLE dbo.roles (
    roleId      INT PRIMARY KEY,
    roleName    VARCHAR(20) UNIQUE NOT NULL
    ) AS NODE

CREATE TABLE dbo.canManage AS EDGE;
CREATE TABLE dbo.isManagedBy AS EDGE;
GO


-- Populate node table
INSERT INTO dbo.roles ( roleId, roleName )
VALUES
    ( 1, 'CEO' ),
    ( 2, 'VP 1' ),
    ( 3, 'VP 2' ),
    ( 4, 'Sales Manager 1' ),
    ( 5, 'Sales Manager 2' ),
    ( 6, 'Ops Manager 1' ),
    ( 7, 'Ops Manager 2' ),
    ( 8, 'Sales Lead 1' ),
    ( 9, 'Salesperson 1' ),
    ( 10, 'Salesperson 2' ),
    ( 11, 'Salesperson 3' )
GO


-- Populate edge table
INSERT INTO dbo.canManage ( $from_id, $to_id )
SELECT ceo.$node_id, VPs.$node_id
FROM dbo.roles ceo
    CROSS JOIN dbo.roles VPs
WHERE ceo.roleName = 'CEO'
  AND VPs.roleName Like 'VP%'


-- VP 1 manages Sales Managers
INSERT INTO dbo.canManage ( $from_id, $to_id )
SELECT a.$node_id, b.$node_id
FROM dbo.roles a
    CROSS JOIN dbo.roles b
WHERE a.roleName = 'VP 1'
  AND b.roleName Like 'Sales Manager%'


-- VP 2 manages Ops Managers
INSERT INTO dbo.canManage ( $from_id, $to_id )
SELECT a.$node_id, b.$node_id
FROM dbo.roles a
    CROSS JOIN dbo.roles b
WHERE a.roleName = 'VP 2'
  AND b.roleName Like 'Ops Manager%'


-- Sales Manger 1 manages Sales Leads
INSERT INTO dbo.canManage ( $from_id, $to_id )
SELECT a.$node_id, b.$node_id
FROM dbo.roles a
    CROSS JOIN dbo.roles b
WHERE a.roleName = 'Sales Manager 1'
  AND b.roleName Like 'Sales Lead%'


-- Sales Leads 1 manages all salespersons
INSERT INTO dbo.canManage ( $from_id, $to_id )
SELECT a.$node_id, b.$node_id
FROM dbo.roles a
    CROSS JOIN dbo.roles b
WHERE a.roleName = 'Sales Lead 1'
  AND b.roleName Like 'Salesperson%'


-- Create the inverse edge / relationship
INSERT INTO dbo.isManagedBy ( $from_id, $to_id )
SELECT $to_id, $from_id
FROM dbo.canManage 
GO



-- Now write the graph queries:

-- Manages
SELECT FORMATMESSAGE( '%s manages %s', r1.roleName, r2.roleName ) manages
FROM dbo.roles r1, dbo.canManage canManage, dbo.roles r2
WHERE MATCH ( r1-(canManage)->r2 )


-- Same manager
SELECT FORMATMESSAGE( '%s and %s have the same manager %s', r1.roleName, r3.roleName, r2.roleName )
FROM dbo.roles r1, dbo.isManagedBy m1, dbo.roles r2, dbo.isManagedBy m2, dbo.roles r3
WHERE MATCH ( r1-(m1)->r2<-(m2)-r3 )
AND r1.$node_id < r3.$node_id



-- Recursive
-- walk the tree ... CEO manages everyone ...
;WITH cte AS (
SELECT 1 xlevel, r1.roleName manager, r2.roleName managed
FROM dbo.roles r1, dbo.canManage canManage, dbo.roles r2
WHERE MATCH ( r1-(canManage)->r2 )
AND r1.roleName = 'CEO'

UNION ALL

SELECT c.xlevel + 1, r1.roleName, r2.roleName
FROM cte c, dbo.roles r1, dbo.canManage canManage, dbo.roles r2
WHERE c.managed = r1.roleName
  AND MATCH ( r1-(canManage)->r2 )
)
SELECT *
FROM cte
ORDER BY xlevel, manager, managed



;WITH cte AS (
SELECT 1 xlevel, r1.roleName manager, r2.roleName managed
FROM dbo.roles r1, dbo.canManage canManage, dbo.roles r2
WHERE MATCH ( r1-(canManage)->r2 )
AND r1.roleName = 'CEO'

UNION ALL

SELECT c.xlevel + 1, c.manager, r2.roleName
FROM cte c, dbo.roles r1, dbo.canManage canManage, dbo.roles r2
WHERE c.managed = r1.roleName
  AND MATCH ( r1-(canManage)->r2 )
)
SELECT *
FROM cte
ORDER BY xlevel, manager, managed