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. 紫书 习题8-9 UVa 1613 (dfs染色+图的性质)

    这道题一开始我没想什么直接开始染, 但是是for循环一个节点一个节点染, 然后就WA 后了看了https://www.cnblogs.com/jerryRey/p/4702323.html 发现原来还 ...

  2. 使用IR2101半桥驱动电机的案例

    作为一个电机驱动开发方面的菜鸟,近日研究了一下通过MOS管对整流后的电源斩波用以驱动直流电机进行调速的方案. 在驱动的过程中,遇到了很多问题,当然也有很多的收获. 写下来以供自己将来查阅,也为其他菜鸟 ...

  3. Material Design学习之 Button(具体分析,传说中的水滴动画)

    转载请注明出处:王亟亟的大牛之路 上一篇大致介绍了Material Design的一些基本概念传送门:http://blog.csdn.net/ddwhan0123/article/details/5 ...

  4. xcode 及 MAC 经常使用快捷键

    郝萌主倾心贡献,尊重作者的劳动成果,请勿转载. 假设文章对您有所帮助,欢迎给作者捐赠,支持郝萌主,捐赠数额任意.重在心意^_^ 我要捐赠: 点击捐赠 Cocos2d-X源代码下载:点我传送 黑苹果键盘 ...

  5. iOS开发之block解析

    1. block的本质是一个Objective-C的对象,为什么这么说? 在Objective-C中,runtime会在执行时依据对象的isa指针的指向,来度额定这个对象的类型,也能够觉得一个对象,它 ...

  6. angularjs ng-app

    <!DOCTYPE HTML> <html ng-app> //ng-app是初始化指令,整个页面都会被angularjs解析,写在div或者其他标签上表示只是局部的div和标 ...

  7. PHP str_replace() 和str_ireplace()函数

    PHP str_replace() 和str_ireplace()函数 实例 把字符串 "Hello world!" 中的字符 "world" 替换为 &quo ...

  8. visual studio code(vscode)的使用(快捷键)

    Visual Studio Code初探 vscode 是一种可运行于 OS X,Windows 和 Linux 之上的免费跨平台编辑器: 1. 快捷键 ctrl + `:调出(对于 windows ...

  9. 性能测试URL自动转码

    最近做性能测试,写了个python程序自动将URL里面的‘%2B’,‘20%’,‘3B'等转换成正常字符,方便查看. import os,sys; path = sys.path[0] os.chdi ...

  10. PostgreSQL Replication之第九章 与pgpool一起工作(1)

    在前面的章节中,我们已经能够深入地理解了pgbouncer,同时也学会了如何使用它来尽可能地优化复制设置.在本章我们将了解一个经常被称作与pgbouncer相对应的工具.尽管pgpool的思想与pgb ...