时间:2021-07-01 10:21:17 帮助过:4人阅读
INSERT INTO #country
SELECT ‘中国‘,‘中国‘,null union all
SELECT ‘江苏‘,‘中国‘,null union all
SELECT ‘南京‘,‘江苏‘,null union all
SELECT ‘无锡‘,‘江苏‘,null union all
SELECT ‘徐州‘,‘江苏‘,null union all
SELECT ‘扬州‘,‘江苏‘,null union all
SELECT ‘苏州‘,‘江苏‘,null union all
SELECT ‘六合区‘,‘南京‘,null union all
SELECT ‘江宁区‘,‘南京‘,null union all
SELECT ‘浦口区‘,‘南京‘,null union all
SELECT ‘仙林区‘,‘南京‘,null union all
SELECT ‘建邺区‘,‘南京‘,null union all
SELECT ‘宝应‘,‘扬州‘,null union all
SELECT ‘仪征‘,‘扬州‘,null union all
SELECT ‘小官庄‘,‘宝应‘,null union all
SELECT ‘范水‘,‘宝应‘,null union all
SELECT ‘鲁垛‘,‘宝应‘,null union all
SELECT ‘安宜‘,‘宝应‘,null union all
SELECT ‘组全‘,‘小官庄‘,null union all
SELECT ‘房桥‘,‘小官庄‘,null union all
SELECT ‘直下沟‘,‘小官庄‘,null union all
SELECT ‘山东‘,‘中国‘,null union all
SELECT ‘济南‘,‘山东‘,null union all
SELECT ‘青岛‘,‘山东‘,null union all
SELECT ‘淄博‘,‘山东‘,null union all
SELECT ‘烟台‘,‘山东‘,null union all
SELECT ‘张店‘,‘淄博‘,null union all
SELECT ‘博山‘,‘淄博‘,null union all
SELECT ‘淄川‘,‘淄博‘,null union all
SELECT ‘龙王山‘,‘浦口区‘,null union all
SELECT ‘高新区‘,‘浦口区‘,null union all
SELECT ‘陆军指挥学院‘,‘浦口区‘,null union all
SELECT ‘南京信息工程大学‘,‘浦口区‘,null union all
SELECT ‘金陵学院‘,‘浦口区‘,null
用CTE递归的方式去实现我们所想要的结果,SQL如下:
WITH CTE AS (
SELECT AreaNam,BelongTo,Msg FROM #country WHERE AreaNam=‘南京‘
UNION ALL
SELECT A.AreaNam,A.BelongTo,A.Msg FROM #country A INNER JOIN CTE B ON A.BelongTo=B.AreaNam
)
SELECT * FROM CTE
AreaNam BelongTo Msg
南京 江苏 NULL
六合区 南京 NULL
江宁区 南京 NULL
浦口区 南京 NULL
仙林区 南京 NULL
建邺区 南京 NULL
龙王山 浦口区 NULL
高新区 浦口区 NULL
陆军指挥学院 浦口区 NULL
南京信息工程大学 浦口区 NULL
金陵学院 浦口区 NULL
SQLServer CTE递归和循环
标签:serve int nbsp 信息 with arc varchar sele where