sql for xml: 生成树型结构的xml文件 (sql for xml to tree )

要得到

Channel    

         account         

               campaign

这样的树型Xml结构


表结构为:




注意:这里有多个 demo的sql语句,效果不同,注意区分!

---- demo sql 1
--select ch.ChannelName as "@Text",
--       (select a.AccountName as "@Text",
--               (select c.CampaignName as "@Text" 
--                from Campaign c
--                where c.AccountId = A.AccountId
--                FOR XML PATH('Campaign'), TYPE
--                )  
--        from Account a
--        where a.ChannelId = ch.ChannelId
--        and a.AccountId <> 0
--        FOR XML PATH('Account'), TYPE
--        )
--from Channel ch
--where ch.ChannelId <> 0
--order by ChannelName
--FOR XML PATH('Channel'), ROOT('Tree')



---- demo sql 2
--select ch.ChannelName as ChannelName,
--       (select a.AccountName as AccountName,
--               (select c.CampaignName as CampaignName 
--                from Campaign c
--                where c.AccountId = A.AccountId
--                FOR XML PATH('Campaign'),TYPE
--                )  
--        from Account a
--        where a.ChannelId = ch.ChannelId
--        and a.AccountId <> 0
--        FOR XML PATH('Account'),TYPE
--        )
--from Channel ch
--where ch.ChannelId <> 0
--order by ChannelName
--FOR XML PATH('Channel'), ROOT('Tree')



---- demo sql 3
--;WITH XMLNAMESPACES (DEFAULT 'http://www.reuters.com/Datascope/ReportRequest.xsd')
--select ch.ChannelName as ChannelName,
--       (select a.AccountName as AccountName,
--               (select c.CampaignName as CampaignName 
--                from Campaign c
--                where c.AccountId = A.AccountId
--                FOR XML PATH('Campaign'),TYPE
--                )  
--        from Account a
--        where a.ChannelId = ch.ChannelId
--        and a.AccountId <> 0
--        FOR XML PATH('Account'),TYPE
--        )
--from Channel ch
--where ch.ChannelId <> 0
--order by ChannelName
--FOR XML PATH('Channel'), ROOT('Tree')



--demo sql 4
select TOP 10
    1 as Tag,
    null as parent,
    ChannelID as [row!1!ChannelID],
    ChannelName as [row!1!ChannelName],
    Description as [row!1!Description!xml]
from Channel 
--for xml raw
FOR XML EXPLICIT,root('tree')



 -- demo sql 5
declare @xml xml
declare @schemaVersion varchar(10)='5.1'  
 
set @xml=
(
	select ch.ChannelName as ChannelName,
		   (select a.AccountName as AccountName,
				   (select c.CampaignName as CampaignName 
					from Campaign c
					where c.AccountId = A.AccountId
					FOR XML PATH('Campaign'),TYPE
					)  
			from Account a
			where a.ChannelId = ch.ChannelId
			and a.AccountId <> 0
			FOR XML PATH('Account'),TYPE
			)
	from Channel ch
	where ch.ChannelId <> 0
	order by ChannelName
	FOR XML PATH('Channel'), ROOT('Tree'),ELEMENTS XSINIL
)

set @xml.modify('insert attribute schemaVersion{sql:variable(''@schemaVersion'')} as last into (/Tree)[1]')   --向根节点添加schemaVersion 属性
select @xml

--SELECT CAST( 
--CAST (('<?xml version="1.0" encoding="iso8859-1"?>'+ cast(@xml  varchar(max)) AS VARBINARY (MAX)) 
-- AS XML)
 --SELECT '<?xml version="1.0" encoding="iso8859-1"?>' + cast(@xml as varchar(max))
  

 


 


生成内容为:


其它:

将字符串分割显示:

declare @phone varchar(20)='1112224444'
select case when len(@phone)=10 
			then substring(@phone,1,3) + '-' + substring(@phone,4,3) + '-' + substring(@phone,7,4) -- 结果:111-222-4444
	else @phone end



参考:

http://blog.csdn.net/iwteih/article/details/2607177

http://www.cnblogs.com/youring2/archive/2008/11/27/1342288.html

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值