这时我有一张tblLocation带有柱子的桌子ID, Location, PartOfID.
该表以递归方式连接到自身: PartOfID -> ID
我的目标是选择输出如下:
> France > Paris > AnyCity >
Run Code Online (Sandbox Code Playgroud)
说明:AnyCity位于巴黎,巴黎位于法国.
我到现在为止找到的解决方案是这样的:
; with q as (
select ID,Location,PartOf_LOC_id from tblLocatie t
where t.ID = 1 -- 1 represents an example
union all
select t.Location + '>' from tblLocation t
inner join q parent on parent.ID = t.LOC_PartOf_ID
)
select * from q
Run Code Online (Sandbox Code Playgroud)
不幸的是我收到以下错误:
使用UNION,INTERSECT或EXCEPT运算符组合的所有查询在其目标列表中必须具有相同数量的表达式.
如果您知道如何修复我的输出,那就太棒了.