SQL?????η????????????
???????????? ???????[ 2015/4/9 12:03:44 ] ????????????? SQL Server ?????
?????????????У???2008???SQL Server????????μ?????????hierarchyid?????????????????????????
????hierarchyid ??????ν??????й??????????????????????????????????????????y???·?????б???
????????·??????????????????????????????????????????С? ???????????б??????????????·???????б?????? ????????μ?????????????????????????????С? ?????????????????????????????????С? ?????????????????б??? ???б???????????????????? ???磬???????????? 1 ????2 ????2 ????3 ???? 3 ??????Ч hierarchyid ·????
???? /
???? /1/
???? /0.3.-7/
???? /1/3/
???? /0.1/0.2/
?????????hierarchyid???????????????CTE??????????????????????????????????????????£?CTE????????????????????????????????PATH??????????????????????hierarchyid?????????????hierarchyid???
?????????????????????????????hierarchyid????????
???????CTE???
????WITH CTEGetChild AS
????(
????SELECT * FROM EL_Organization.Organization WHERE ID='ecc43c7159924dca91e2916368f923f4' --and [State]=0 and AuditState=2
????UNION ALL
????(
????SELECT A.* FROM EL_Organization.Organization AS A
????INNER JOIN CTEGetChild AS B ON a.PARENTID=B.ID --and A.[State]=0 and A.AuditState=2
????)
????)
???????????4489?У????25S??
????????CTE???????????????????????????????????????????????????
????????hierarchyid
?????????????????????Σ??????丳???
create function f_cidname(@id varchar(50)) returns varchar(max) as
begin
declare @pids nvarchar(max);
declare @pNames nvarchar(max);
set @pids='';
set @pNames='';
with cte as
( select id??parentid??name from EL_Organization.Organization where id =@id--'00037fdf184e48d084b87c3499e3c0e5'
union all
select b.id??b.parentid??b.name from cte A ??EL_Organization.Organization B where a.parentid = b.id
)
select @pids=convert(varchar(32)??Convert(int?? Convert(varbinary(max)?? id))) + '/'+ @pids from cte
return '/'+@pids
end
go
????????????????Update????
????UPDATE EL_Organization.Organization SET PIDS=dbo.f_cidname(id)
???????id??guid??32λ?????????hierarchyid??β??????????Path??·?????????????GUID?????????convert(varchar(32)??Convert(int?? Convert(varbinary(max)?? id)))
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11