我尝试在 SQL Server 中使用图形功能。现在我遇到了软删除问题。
我有以下图表
[1] -> [2] -> [9 (deleted)] -> [4]
当我运行以下脚本时
CREATE TABLE MyNode
(
[Id] [bigint] NOT NULL,
[IsDeleted] [bit] NOT NULL,
) AS NODE;
CREATE TABLE MyEdge
(
State [int] NOT NULL
) AS EDGE;
INSERT INTO MyNode (Id, IsDeleted)
VALUES (1, 0), (2, 0), (4, 0), (9, 1);
INSERT INTO MyEdge
VALUES
( (SELECT $node_id FROM MyNode WHERE Id = 1), (SELECT $node_id FROM MyNode WHERE Id = 2), 1),
( (SELECT $node_id FROM MyNode WHERE Id = 2), (SELECT $node_id FROM MyNode WHERE Id = 9), 1),
( (SELECT $node_id FROM MyNode WHERE Id = 9), (SELECT $node_id FROM MyNode WHERE Id = 4), 1)
;
SELECT
src.Id ID_SOURCE
, LAST_VALUE(trgt.Id) WITHIN GROUP (GRAPH PATH) AS ID_TARGET
, STRING_AGG(trgt.Id, '->') WITHIN GROUP (GRAPH PATH) AS ID_CHAIN
--, STRING_AGG(compare.State, '->') WITHIN GROUP (GRAPH PATH) AS STATE_CHAIN
--, STRING_AGG(trgt.IsDeleted, '->') WITHIN GROUP (GRAPH PATH) AS DELETED_CHAIN
FROM
MyNode AS src
, ( SELECT
*
FROM
MyEdge
WHERE
State = 1
) FOR PATH AS compare
, ( SELECT
*
FROM
MyNode
WHERE
IsDeleted = 0
) FOR PATH AS trgt
WHERE
MATCH ( SHORTEST_PATH( src(-(compare)->trgt)+ ) )
AND src.Id = 1;
SELECT
src.Id AS SOURCE_ID
, ed.State AS EDGE_STATE
, trgt.Id AS TARGET_ID
FROM
MyNode AS src
, MyEdge AS ed
, MyNode AS trgt
WHERE
MATCH( src-(ed)->trgt )
AND src.Id = 2;
DROP TABLE MyNode;
DROP TABLE MyEdge;
(提示:这只适用于 SQL-Server 2019)
我得到以下结果
ID_SOURCE | ID_TARGET | ID_CHAIN |
---|---|---|
1 | 2 | 2 |
1 | 4 | 2->4 |
没有优势2->4
,但是2->9->4
。但是节点 9 被删除,因此它用于图遍历,但在输出中被抑制。
这是 SQL-Server 中的错误还是我做错了什么?
还是有另一种方法我应该使用 Graph-DB 的软删除?