樹形資料生成XML

2021-06-26 03:53:19 字數 3217 閱讀 9405

--樹形資料生成xml

if exists (select * from dbo.sysobjects where id = object_id(n'[dbo].[f_xml_lr]') and xtype in (n'fn', n'if', n'tf'))

drop function [dbo].[f_xml_lr]

goif exists (select * from dbo.sysobjects where id = object_id(n'[dbo].[f_xmlend_mark]') and xtype in (n'fn', n'if', n'tf'))

drop function [dbo].[f_xmlend_mark]

go/*--補充生成樹形資料 xml 尾部標誌

配合生成 xml 完整版函式使用

create function f_xmlend_mark(

@p_level int,   --上條記錄的層次

@level int              --本條記錄的層次

)returns nvarchar(800)

asbegin

declare @r nvarchar(800)

set @r=''

while @p_level>@level

select @p_level=@p_level-1

,@r=@r+space(@p_level*4)+''

+char(13)+char(10)

return(@r)

endgo

/*--生成樹形資料 xml 標誌

生成樹形資料生成 xml 文件需要的必要標誌

採用格式化處理方法,生成的資料帶有縮排表示

create function f_xml_lr()

returns @re table(sid int identity,id varchar(10),level int,xml_l nvarchar(1000),xml_r nvarchar(1000))

asbegin

--生成排序後的編號列表

declare @t table(id varchar(10),level int,sid varchar(8000))

declare @l int,@sid int

set @l=1

insert @t select [id],@l,id

from [tb]

where [pid]=0

while @@rowcount>0

begin

set @l=@l+1

insert @t select a.[id],@l,b.sid+','+a.[id]

from [tb] a,@t b

where a.[pid]=b.id and b.level=@l-1

endinsert @re(xml_l)

select '<?xml version="1.0" encoding="gb2312"?>'

union all select ''

insert @re(id,level,xml_l,xml_r)

select a.id,a.level

,space(a.level*4)+',case when b.id is null then '>' else '/>' end

from @t a left join(

select id=[id] from [tb] aa

where not exists(

select 1 from [tb] where [pid]=aa.[id])

)b on a.id=b.id

order by a.sid

select @sid=id,@l=level from @re where sid=scope_identity()

update a set xml_l=dbo.f_xmlend_mark(b.level,a.level)+a.xml_l

from @re a,@re b

where a.leveland a.sid=b.sid+1

if @l>1

update @re set xml_r=xml_r+char(13)+char(10)+dbo.f_xmlend_mark(@l,1)

where sid=@sid

insert @re(xml_l) select ''

return

endgo

--示例資料

create table [tb](id varchar(10),pid varchar(10),name varchar(20))

insert [tb] select '01',''  ,'中國'

union all select '02',''  ,'美國'

union all select '03',''  ,'加拿大'

union all select '04','01','北京'

union all select '05','01','上海'

union all select '06','01','江蘇'

union all select '07','06','蘇州'

union all select '08','07','常熟'

union all select '09','06','南京'

union all select '10','06','無錫'

union all select '11','02','紐約'

union all select '12','02','舊金山'

go--呼叫生成xml樹

select  re=case

when a.[id] is null

then b.xml_l

else b.xml_l+' id="'+a.[id]+'" name="'+a.[name]+'"'    +b.xml_r

endfrom [tb] a

right join f_xml_lr() b on a.[id]=b.id

order by b.sid

go--刪除測試

drop table [tb]

--drop function f_xmlend_mark,f_xml_lr

/*--測試結果

re                                           

<?xml version="1.0" encoding="gb2312"?>

sql樹形資料生成xml

樹形資料生成xml if exists select from dbo.sysobjects where id object id n dbo f xml lr and xtype in n fn n if n tf drop function dbo f xml lr goif exists se...

sql樹形資料生成xml

樹形資料生成xml if exists select from dbo.sysobjects where id object id n dbo f xml lr and xtype in n fn n if n tf drop function dbo f xml lr goif exists se...

樹形資料轉換

測試資料 create table project id int,name nvarchar 20 parent id int insert project select 1,所有專案 null union all select 2,專案1 1 union all select 3,專案2 1 cr...