我正在尝试执行以下操作但获取最大递归错误。有人可以帮忙吗?
示例代码,用于演示我正在尝试实现的目标:
DECLARE @SecurityMaster AS TABLE
(
ID INT,
SecurityAlias INT,
LegNumber INT
)
INSERT INTO @SecurityMaster
SELECT 12829, 3030106, NULL
UNION ALL
SELECT 12829, 3030107, 1
SELECT * FROM @SecurityMaster;
WITH CTE1 (ID, SecurityAlias, LegNumber)
AS
(
SELECT S.ID, S.SecurityAlias, S.LegNumber
FROM @SecurityMaster S
WHERE S.LegNumber IS NOT NULL
UNION ALL
select s.ID, s.SecurityAlias, s.LegNumber
from @SecurityMaster S inner join CTE1 c on s.ID = c.ID
where s.LegNumber is NULL
)
SELECT *
FROM CTE1;
结果我期待:
ID SecurityAlias LegNumber
-----------------------------------------
12829 3030107 1
12829 3030106 NULL
答案 0 :(得分:0)
你的问题很难理解,但这对你有用吗?
select s.ID, s.SecurityAlias, s.LegNumber
from @SecurityMaster S
where s.LegNumber is NULL
And s.id in (SELECT f.ID
FROM @SecurityMaster f
WHERE f.LegNumber IS NOT NULL)