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. How Javascript works (Javascript工作原理) (五) 深入理解 WebSockets 和带有 SSE 机制的HTTP/2 以及正确的使用姿势

    个人总结: 1.长连接机制——分清Websocket,http2,SSE: 1)HTTP/2 引进了 Server Push 技术用来让服务器主动向客户端缓存发送数据.然而,它并不允许直接向客户端程序 ...

  2. 【codeforces 22C】 System Administrator

    [题目链接]:http://codeforces.com/problemset/problem/22/C [题意] 给你n个点; 要求你构造一个含m条边的无向图; 使得任意两点之间都联通; 同时,要求 ...

  3. POJ 3155 Hard Life

    Hard Life Time Limit: 8000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID:  ...

  4. 调用Windows属性窗口

    简述 在Windows系统下.可以通过:右键 -> 属性,来查看文件/文件夹对应的属性信息,包括:常规.安全.详细信息等. 简述 共有类型 共有类型 首先,需要包含头文件: #include & ...

  5. [React Native] Dismiss the Keyboard in React Native

    In this lesson you will learn how create a re-usable component that gives the user the ability to di ...

  6. hdu 2037 贪心

    今年暑假不AC Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  7. sass09

    scss /* 1.使用自定义function和@each实现栅格布局. @function buildLayout($num: 5){ } 结果: col1{width: 20%} col2{wid ...

  8. angularjs 自定义服务

    <!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="C ...

  9. Random words

    To choose a random word from the histogram, the simplest algorithm is to build a list with multiple ...

  10. Linux安装PHP MongoDB扩展

    本文将讲述一下本人安装MongoDB扩展的过程,大家可以略作参考 安装环境 Linux环境:CentOS 6.5 Apache版本:2.4 PHP版本:5.4.3 MongoDB版本:2.6.5 一. ...