有沒有辦法檢查輸出中是否有循環?避免循環輸出
實施例:
CREATE TABLE packages
(
request varchar(10),
depend varchar(10)
);
INSERT INTO packages
(request, depend)
VALUES
('R001', 'R990'),
('R001', 'R992'),
('R002', 'R991'),
('R002', 'R990'),
('R990', 'R001'),
('R010', 'R001'),
('R215', 'R001'),
('R990', 'R887'),
('R990', 'R886');
SELECT
request,
depend
FROM
(
SELECT request, depend FROM packages WHERE request = 'R001'
UNION ALL
SELECT request, depend FROM packages WHERE depend IN ('R001')
) as Report
的最終結果是:
request depend
1 R001 R990
2 R001 R992
3 R990 R001
4 R010 R001
5 R215 R001
線3是從第1行週期,因此第3行不應該在的結果。
預期的結果:
request depend
1 R001 R990
2 R001 R992
3 R010 R001
4 R215 R001
感謝。
可以 –
你必須使用[遞歸查詢(HTTPS添加預期的結果://technet.microsoft.com/en-us/library/ms186243(v = sql.105).aspx) –
您是否只考慮大小爲1的循環?或者可以是更大的週期? –