sql for xml: 生成树型结构的xml文件 (sql for xml to tree )
来源:互联网 发布:学软件开发有前途吗 编辑:程序博客网 时间:2024/05/21 10:23
要得到
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 4select 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 rawFOR XML EXPLICIT,root('tree') -- demo sql 5declare @xml xmldeclare @schemaVersion varchar(10)='5.1' set @xml=(select ch.ChannelName as ChannelName, (select a.AccountName as AccountName, (select c.CampaignName as CampaignName from Campaign cwhere c.AccountId = A.AccountIdFOR XML PATH('Campaign'),TYPE) from Account awhere a.ChannelId = ch.ChannelIdand a.AccountId <> 0FOR XML PATH('Account'),TYPE)from Channel chwhere ch.ChannelId <> 0order by ChannelNameFOR 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-4444else @phone end
参考:
http://blog.csdn.net/iwteih/article/details/2607177
http://www.cnblogs.com/youring2/archive/2008/11/27/1342288.html
0 0
- sql for xml: 生成树型结构的xml文件 (sql for xml to tree )
- SQL FOR XML的应用
- SQL FOR XML
- sql For XML Path
- sql for xml path
- SQL FOR XML PATH
- SQL Server FOR XML
- sql for xml path
- SQL Server 的For XML及sp_xml_preparedocument
- SQL FOR XML子句的各种用法
- SQL FOR XML子句的各种用法
- SQL for xml path使用
- SQL SERVER FOR XML PATH
- Sql中的for xml用法
- sql for xml path用法
- SQL FOR XML PATH 用法
- SQL Server 2005 For XML
- SQL Server FOR XML PATH
- JAVA注解
- (二)函数定义文件
- Apache Hadoop 的最佳实践和反模式
- Windows下Sleep()
- (三)线性表菜单
- sql for xml: 生成树型结构的xml文件 (sql for xml to tree )
- opencv打开摄像头为空的解决方案
- FFmpeg安装,AAC编码器安装
- EXCEL VBA Macro sourcecode output
- HPUX配置APA网卡聚合
- 错排和组合,考新郎官
- 如何恢复摔坏了硬盘文件呢
- 各种变量的存储类型
- Android 学习笔记——利用JNI技术在Android中调用、调试C++代码