CTE不使用额外的连接子句

时间:2012-09-06 20:50:35

标签: sql-server tsql

当我为[PropertyId]添加额外的连接信息时,有人可以告诉我为什么这会返回0条记录吗?我知道这很简单,但我很茫然。

declare @ViewId as int;

set @ViewId = 11;

with Views
as
(
    select [LiveWireView].[Id], [LiveWireView].[Name], [LiveWireView].[ParentId], [PropertyToViewMap].[PropertyId]
    from [LiveWireView]
    inner join [PropertyToViewMap] on [PropertyToViewMap].[ViewId] = [LiveWireView].[Id]
    where [LiveWireView].[Id] = @ViewId
    union all
    select [v].[Id], [v].[Name], [v].[ParentId], [p].[PropertyId]
    from [LiveWireView] v
    inner join [PropertyToViewMap] p on [p].[ViewId] = [v].[Id]
    inner join Views on Views.Id = v.ParentId
)

select [PropertyId] from Views;

1 个答案:

答案 0 :(得分:0)

如果您添加了此加入:

inner join [PropertyToViewMap] p on [p].[ViewId] = [v].[Id]

并且不再获得结果,我认为说没有符合这些条件的记录也是一样的。您可以将其转换为LEFT JOIN并查看PropertyId

是否为空值来测试