1.sqlserver中一种查父节点id的方法。
方法不难,其实就是 递归调出所有的父节点,但是把不懂这个原理怎么实现的,看着临时表mt可以用在union all 里本身还是有点懵的,暂时先记录一下这个方法
with mt as
(select t.ST_ID , t.ST_Name , t.ST_PID
from AWeb_SupplyType t where ST_ID =10051
union all
select t.ST_ID , t.ST_Name , t.ST_PID from AWeb_SupplyType t , mt
where t.ST_ID = mt.ST_PID )
select ST_ID , ST_Name , ST_PID from mt where ST_PID=0
2.sqlserver合并多条数据
FOR XML PATH 具体来讲是将查询结果集以XML形式
基本用法是输出时行节点可由row变为MyHobby ,列名由字段名变为别名
SELECT CountryCode as 'Code', ContientType as 'Type' FROM PR_ServerCountry FOR XML PATH ('MyHobby')
将其用于表中,可将xml的格式转换为列表格式path(")就是讲行节点row置为''
select stuff((select ','+CountryCode from PR_ServerCountry for xml path('')),1,1,'') as name