nodes() Method (xml Data Type)

https://docs.microsoft.com/en-us/sql/t-sql/xml/nodes-method-xml-data-type

The nodes() method is useful when you want to shred an xml data type instance into relational data.

It allows you to identify nodes that will be mapped into a new row.

Every xml data type instance has an implicitly provided context node.

For the XML instance stored in a column or variable, this is the document node.

The document node is the implicit node at the top of every xml data type instance.

The result of the nodes() method is a rowset that contains logical copies of the original XML instances.

In these logical copies, the context node of every row instance is set to one of the nodes identified with the query expression, so that subsequent queries can navigate relative to these context nodes.

You can retrieve multiple values from the rowset.

For example, you can apply the value() method to the rowset returned by nodes() and retrieve multiple values from the original XML instance.

Note that the value() method, when applied to the XML instance, returns only one value.

Syntax

nodes (XQuery) as Table(Column)  

Arguments

XQuery
Is a string literal, an XQuery expression.

If the query expression
constructs nodes, these constructed nodes are exposed in the resulting
rowset.

If the query expression results in an empty sequence, the rowset
will be empty.

If the query expression statically results in a sequence
that contains atomic values instead of nodes, a static error is raised.

Table(Column)
Is the table name and the column name for the resulting rowset.

Example

 DECLARE @UsedRecords XML;
SET @UsedRecords = '<Record ID="107" /><Record ID="116" /><Record ID="410" />'; SELECT Result.Id.value(
'@ID' ,
'int'
)
FROM @UsedRecords.nodes('/Record') AS Result(Id)

query() Method (xml Data Type)

https://docs.microsoft.com/en-us/sql/t-sql/xml/query-method-xml-data-type

declare @myDoc xml
set @myDoc = '<Root>
<ProductDescription ProductID="1" ProductName="Road Bike">
<Features>
<Warranty>1 year parts and labor</Warranty>
<Maintenance>3 year parts and labor extended maintenance is available</Maintenance>
</Features>
</ProductDescription>
</Root>'
SELECT @myDoc.query('/Root/ProductDescription/Features')
DECLARE @Propertis XML;
SELECT @Propertis = WebPartProperties
FROM dbo.CMS_WebPart
WHERE WebPartName = 'LISA_GiftBrowser';
SELECT @Propertis;
SELECT @Propertis.query('/form/field/settings/controlname')

参考

https://stackoverflow.com/questions/15680259/parse-xml-in-sql-server

DECLARE @xml xml
SET @xml =
'<GespeicherteDaten>
<strategieWuerfelFelder Type="strategieWuerfelFelder">
<Felder X="3" Y="3" Z="3">
<Feld X="1" Y="1" Z="1">
<strategieWuerfelFeld Type="strategieWuerfelFeld">
<Name>Name</Name>
<Beschreibung>Test</Beschreibung>
</strategieWuerfelFeld>
</Feld>
<Feld X="1" Y="1" Z="2">
<strategieWuerfelFeld Type="strategieWuerfelFeld">
<Name>Name2</Name>
<Beschreibung>Test2</Beschreibung>
</strategieWuerfelFeld>
</Feld>
</Felder>
</strategieWuerfelFelder>
</GespeicherteDaten>' SELECT
b.value('@X', 'int') as X
, b.value('@Y', 'int') as Y
, b.value('@Z', 'int') as Z
, b.value('(./strategieWuerfelFeld/Name/text())[1]','Varchar(50)') as [Name]
, b.value('../@X','int') as Felder_X
, b.value('../@Y','int') as Felder_Y
, b.value('../@Z','int') as Felder_Z
FROM @xml.nodes('/GespeicherteDaten/strategieWuerfelFelder/Felder/Feld') as a(b)
<field column="CoverAllGiftBrand" fieldcaption="Cover All Brand" visible="true" columntype="text" fieldtype="CustomUserControl" allowempty="true" columnsize="50" fielddescription="A brand which can cover all brands of gift" publicfield="false" guid="734db2ca-5ce3-4326-a5c8-a821631e09ae" visibility="none">
<settings>
<controlname>textboxcontrol</controlname>
</settings>
</field>
DECLARE @Propertis XML;
DECLARE @Result XML;
SELECT @Propertis = WebPartProperties
FROM dbo.CMS_WebPart
WHERE WebPartName = 'LISA_GiftQuickSearch';
SELECT
b.value('@fieldcaption','nvarchar(50)') AS Property
,b.value('@column','nvarchar(50)') AS ColumnName
,b.value('(./settings/controlname/text())[1]', 'nvarchar(50)') AS ControlType
FROM @Propertis.nodes('/form/field') AS a(b)
WHERE b.value('(./settings/controlname/text())[1]', 'nvarchar(50)') LIKE '%lisa%'
ORDER BY ControlType;
DECLARE @Propertis XML;
DECLARE @Result XML;
SELECT @Propertis = WebPartProperties
FROM dbo.CMS_WebPart
WHERE WebPartName = 'LISA_GiftBrowser';
SELECT
DISTINCT b.value('(./settings/controlname/text())[1]', 'nvarchar(100)') AS ControlType
FROM @Propertis.nodes('/form/field') AS a(b)
WHERE b.value('(./settings/controlname/text())[1]', 'nvarchar(100)') LIKE '%lisa%'
ORDER BY ControlType;

Parse List

DECLARE @Parameters XML = '
<Request xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Orders>
<Order>
<Column>Column1</Column>
<Direction>Asc</Direction>
</Order>
<Order>
<Column>Column2</Column>
<Direction>Desc</Direction>
</Order>
</Orders>
<AccountId>1</AccountId>
</Request>
';
DECLARE @AccountId INT; SELECT @AccountId = b.value('(./AccountId/text())[1]', 'INT')
FROM @Parameters.nodes('/Request') AS a(b);
SELECT @AccountId AS AccountId SELECT b.value('(./Column/text())[1]', 'NVARCHAR(50)') AS ColumnName ,
b.value('(./Direction/text())[1]', 'NVARCHAR(50)') AS Direction
FROM @Parameters.nodes('/Request/Orders/Order') AS a(b);

parse audit criteria

DECLARE @Params XML
= N'<AuditCriteria xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><ProgramName /><UserName /><ActionType /></AuditCriteria>'; SELECT b.value('(./ProgramName/text())[1]', 'NVarchar(max)'),
b.value('(./UserName/text())[1]', 'NVarchar(max)'),
b.value('(./ActionType/text())[1]', 'NVarchar(max)')
FROM @Params.nodes('/AuditCriteria') AS a(b);

xml Data Type Methods in sql server的更多相关文章

  1. XML Data Type Methods(一)

    XML Data Type Methods(一) /*XML Data Type Methods: 1.The query('XQuery') method retrieves(vt.检索,重新得到) ...

  2. SQL Server error "Xml data type is not supported in distributed queries" and workaround for it

    Recently while working with data migration,got an error while running a following query where Server ...

  3. 解决VS2010在新建实体数据模型出现“在 .NET Framework Data Provider for Microsoft SQL Server Compact 3.5 中发生错误。请与提供程序供应商联系以解决此问题。”的问题

    最近想试着学习ASP.NET MVC,在点击 添加--新建项--Visual C#下的数据中的ADO.NET 实体数据模型,到"选择您的数据连接"时,出现错误,"在 .N ...

  4. import data from excel to sql server

    https://www.c-sharpcorner.com/article/how-to-import-excel-data-in-sql-server-2014/ 需要注意的是,第一次是选择sour ...

  5. 在 .NET Framework Data Provider for Microsoft SQL Server Compact 3.5 中发生错误

    32位机器删除 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\version\DataProviders\{7C602B5B-ACCB-4acd ...

  6. Querying Microsoft SQL Server 2012 读书笔记:查询和管理XML数据 2 -使用XQuery 查询XML数据

    XQuery 是一个浏览/返回XML实例的标准语言. 它比老的只能简单处理节点的XPath表达式更丰富. 你可以同XPath一样使用.或是遍历所有节点,塑造XML实例的返回等. 作为一个查询语言, 你 ...

  7. Querying Microsoft SQL Server 2012 读书笔记:查询和管理XML数据 1 -使用FOR XML返回XML结果集

    XML 介绍 <CustomersOrders> <Customer custid="1" companyname="Customer NRZBB&qu ...

  8. SQL Server ->> XML方法

    1. 得到XML类型中某个节点下子节点的数量 DECLARE @xml xml SET @xml = ' <Parameters> <Parameter name = "p ...

  9. SQL SERVER中XML查询:FOR XML指定PATH

    SQL SERVER中XML查询:FOR XML指定PATH 前言 在SQL SERVER中,XML查询能够指定RAW,AUTO,EXPLICIT,PATH.本文用一些实例介绍SQL SERVER中指 ...

随机推荐

  1. [HEOI2012]采花(树状数组+离线)

    听说这题的所发和HH的项链很像. 然而那道题我使用莫队写的... 这是一个套路,pre数组加升维(在线). 记录一个\(pre\)数组,\(pre[i]\)代表上一个和i颜色相同的下标. 我们把询问离 ...

  2. HDU 1667 The Rotation Game (A*迭代搜索)

    题目大意:略 每次选择一个最大深度K,跑IDA* 估价函数H=8-中间8个格里出现次数最多的数的个数x,即把它填满这个数最少需要8-x次操作,如果dep+H>K,就跳出.. 深搜的时候暴力修改, ...

  3. windows下命令行复制

    在CMD命令提示符窗口中点击鼠标右键,选择“标记”选项,然后按住鼠标左键不动,拖动鼠标标记想要复制的内容.标记完成以后请按键盘上的“回车”键

  4. 原创全新打包工具Parcel零配置VueJS开发脚手架

    parcel-vue 一个基于Parcel打包工具的 VueJS急速开发脚手架解决方案,强烈建议使用node8.0以上 项目地址: https://github.com/w3c-king/p... 初 ...

  5. root用户无法切换到cdh的hive账号上

    在/etc/passwd中看到hive账号是登录的终端是/bin/false,而正常的用户配置的都是/bin/bash,因此在root账号su到hive也是没有用的 hive:x:111:111:Hi ...

  6. hdu3468 Treasure Hunting 二分匹配

    //给一个n*m的图 //.表示空白地 //*表示有黄金 //#表示墙 //一个人须要依照A...Z..a..z的顺序以最短路径走到下一个 //每次仅仅能在他的路线上经过的地方取一块黄金 //问最多能 ...

  7. 三层登录—c#

    学习了三层,有一个登录窗口的小练习.是我们第一次接触三层的初战.如今仅仅是简单的了解了一些,须要学习的还有非常多,以下浅谈自己的理解. 我们说的三层就是分层了显示层.业务逻辑层和数据訪问层.当中显示层 ...

  8. MYSQL源代码编译的变动

    Mysql的安装,对于mysql不同版本号的mysql源代码编译方式不一样 5.6.2的版本号開始编译方式已经由 configure 变成了cmake方式 ,相关的新的 编译方式在mysql官网已经提 ...

  9. neat算法——本质就是遗传算法用于神经网络的自动构建

    基于NEAT算法的马里奥AI实现 所谓NEAT算法即通过增强拓扑的进化神经网络(Evolving Neural Networks through Augmenting Topologies),算法不同 ...

  10. Mvc 返回文件直接下载

    今天碰到一个问题,前端点击下载文件,后端判断文件是否存在,不存在则自动生成文件(图片),返回前端会自动下载文件 网上查了一些  Mvc  action中返回File类型 设置一些contentType ...