SQL Server递归查询获取折线3D点报错求助
问题排查与修复
错误原因
递归CTE的**递归分支(UNION ALL下方的查询)**中引用了shape列,但你定义的cte_coords仅包含n和point两个字段,未保留原表的几何对象shape,因此SQL Server无法识别该列。另外硬编码n < 130存在风险:若折线实际点数少于130会产生无效调用,多于130则会丢失部分点。
修复方案
- 在CTE的锚点查询中添加
shape字段,让递归分支能持续访问几何对象 - 用折线的实际点数
shape.STNumPoints()替代固定值130,确保遍历所有点 - 调整起始序号为0,匹配你期望的输出格式
修正后的代码
with cte_coords (n, shape, point) as ( select 0 ,shape ,shape.STPointN(1).ToString() as point from [ourArcGISEnterpriseServer].[Schema].[myTableWithPolylines] where OBJECTID = 316101 union all select n + 1 ,shape ,shape.STPointN(n+2).ToString() as point -- STPointN从1开始计数,序号n对应点索引n+1 from cte_coords where n + 1 < shape.STNumPoints() -- 用实际点数判断,避免超出范围 ) select n, point from cte_coords;
关键说明
- CTE新增
shape字段,保证递归过程中能调用几何对象的方法 STPointN的参数是从1开始的索引,因此序号n对应的点索引为n+1,递归时需对应调整参数- 用
shape.STNumPoints()作为终止条件,确保不会遗漏或超出折线的实际点数量
内容的提问来源于stack exchange,提问作者tlange
相关产品推荐
相关产品推荐

