惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Microsoft Azure Blog
Microsoft Azure Blog
S
Securelist
V
Vulnerabilities – Threatpost
C
Cyber Attacks, Cyber Crime and Cyber Security
Schneier on Security
Schneier on Security
Cyberwarzone
Cyberwarzone
Simon Willison's Weblog
Simon Willison's Weblog
Hacker News - Newest:
Hacker News - Newest: "LLM"
P
Palo Alto Networks Blog
T
Troy Hunt's Blog
SecWiki News
SecWiki News
Security Archives - TechRepublic
Security Archives - TechRepublic
T
The Blog of Author Tim Ferriss
Project Zero
Project Zero
Microsoft Security Blog
Microsoft Security Blog
The Register - Security
The Register - Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
J
Java Code Geeks
F
Full Disclosure
阮一峰的网络日志
阮一峰的网络日志
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Attack and Defense Labs
Attack and Defense Labs
Know Your Adversary
Know Your Adversary
WordPress大学
WordPress大学
PCI Perspectives
PCI Perspectives
N
News | PayPal Newsroom
The Last Watchdog
The Last Watchdog
酷 壳 – CoolShell
酷 壳 – CoolShell
P
Privacy & Cybersecurity Law Blog
P
Proofpoint News Feed
V
Visual Studio Blog
C
CERT Recently Published Vulnerability Notes
H
Help Net Security
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
云风的 BLOG
云风的 BLOG
月光博客
月光博客
T
The Exploit Database - CXSecurity.com
I
InfoQ
大猫的无限游戏
大猫的无限游戏
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
U
Unit 42
腾讯CDC
小众软件
小众软件
V2EX - 技术
V2EX - 技术
罗磊的独立博客
Cloudbric
Cloudbric
Recorded Future
Recorded Future
IT之家
IT之家
Google DeepMind News
Google DeepMind News
C
CXSECURITY Database RSS Feed - CXSecurity.com

博客园 - 挑战

从存储过程中读取相关信息 Blend Step by Step书籍笔记(第一章) WPF非轮询方式实时更新数据库变化SqlDependency 动态解析XAML文本构建WPF的UI 解决为'*********' 的游标已存在问题 数据表死锁查询和处理 SQL Server操作XML(六)XML FLOWR SQL Server操作XML(五)XML Query-XQuery SQL Server操作XML(四)XML数据类型 SQL Server操作XML(三)OPENXML函数功能 SQL Server操作XML(二)XML子句实例 数据绑定 最为详尽的WPF类继承关系 LinQ数据访问 WPF Diagram Designer Part 3:连接Item 照猫画虎WPF之二数据绑定 照猫画虎WPF之一:命名空间 解决WPF部署后客户端访问安全性问题 C#读取文本播放相应语音
SQL Server操作XML(一)XML子句
挑战 · 2012-06-09 · via 博客园 - 挑战

一、以记录为核心

(1)path模式

select *

from 表名
for xml path

执行结果

<row>
  <TreeNode_ID>2</TreeNode_ID>
  <TreeNode_Name>1</TreeNode_Name>
 </row>

(2)raw模式

select *

from 表名
for xml raw

执行结果

<row TreeNode_ID="2" TreeNode_Name="1"  />

(3)auto模式

select *

from 表名
for xml auto

执行结果

<td_Tree_Node TreeNode_ID="2" TreeNode_Name="1"  />

二、以元素为核心

(1)path模式

select *

from 表名
for xml path,elements

执行结果

<row>
  <TreeNode_ID>2</TreeNode_ID>
  <TreeNode_Name>1</TreeNode_Name>
</row>

(2)Raw 模式

select *
from 表名
for xml raw,elements

执行结果

<row>
  <TreeNode_ID>2</TreeNode_ID>
  <TreeNode_Name>1</TreeNode_Name>
</row>

(3)Auto模式

select *
from 表名
for xml auto,elements

执行结果

<td_Tree_Node>
  <TreeNode_ID>2</TreeNode_ID>
  <TreeNode_Name>1</TreeNode_Name>
</td_Tree_Node>

三、可空支持

select *
from 表名

for xml path,elements xsinil

 执行结果

<row xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
  <TreeNode_ID>2</TreeNode_ID>
  <TreeNode_Name>1</TreeNode_Name>
  <Parent_ID xsi:nil="true" />

</row>

四、附加Schema结构

select *
from 表名

for xml auto ,xmlschema

执行结果

除了基本数据外,增加Schema结构的XML表述,如下所示

<xsd:schema targetNamespace="urn:schemas-microsoft-com:sql:SqlRowSet1" xmlns:schema="urn:schemas-microsoft-com:sql:SqlRowSet1" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:sqltypes="http://schemas.microsoft.com/sqlserver/2004/sqltypes" elementFormDefault="qualified">
  <xsd:import namespace="http://schemas.microsoft.com/sqlserver/2004/sqltypes" schemaLocation="http://schemas.microsoft.com/sqlserver/2004/sqltypes/sqltypes.xsd" />
  <xsd:element name="td_Tree_Node">
    <xsd:complexType>
      <xsd:attribute name="TreeNode_ID" type="sqltypes:int" use="required" />
      <xsd:attribute name="TreeNode_Name" use="required">
        <xsd:simpleType>
          <xsd:restriction base="sqltypes:varchar" sqltypes:localeId="2052" sqltypes:sqlCompareOptions="IgnoreCase IgnoreKanaType IgnoreWidth">
            <xsd:maxLength value="50" />
          </xsd:restriction>
        </xsd:simpleType>
      </xsd:attribute>
       </xsd:complexType>
  </xsd:element>
</xsd:schema>

五、Root:自定制根元素名称

select *
from td_Tree_Node
for xml auto ,root('根节点')

执行结果

<根节点> 

    <td_Tree_Node TreeNode_ID="2" TreeNode_Name="1" />
</根节点>
六、Type:指明返回XML数据类型的结果,多用于嵌套查询

七、Path模式下XML格式化

select TreeNode_ID as "@TreeNode_ID",
       TreeNode_Name as  "Details/@Name",
       TreeNode_Code as "Details/text()"
from td_Tree_Node
for xml path

执行结果

<row TreeNode_ID="2">
  <Details Name="1">1</Details>
</row>