xml Data Type Methods in sql server
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的更多相关文章
- XML Data Type Methods(一)
XML Data Type Methods(一) /*XML Data Type Methods: 1.The query('XQuery') method retrieves(vt.检索,重新得到) ...
- 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 ...
- 解决VS2010在新建实体数据模型出现“在 .NET Framework Data Provider for Microsoft SQL Server Compact 3.5 中发生错误。请与提供程序供应商联系以解决此问题。”的问题
最近想试着学习ASP.NET MVC,在点击 添加--新建项--Visual C#下的数据中的ADO.NET 实体数据模型,到"选择您的数据连接"时,出现错误,"在 .N ...
- import data from excel to sql server
https://www.c-sharpcorner.com/article/how-to-import-excel-data-in-sql-server-2014/ 需要注意的是,第一次是选择sour ...
- 在 .NET Framework Data Provider for Microsoft SQL Server Compact 3.5 中发生错误
32位机器删除 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\version\DataProviders\{7C602B5B-ACCB-4acd ...
- Querying Microsoft SQL Server 2012 读书笔记:查询和管理XML数据 2 -使用XQuery 查询XML数据
XQuery 是一个浏览/返回XML实例的标准语言. 它比老的只能简单处理节点的XPath表达式更丰富. 你可以同XPath一样使用.或是遍历所有节点,塑造XML实例的返回等. 作为一个查询语言, 你 ...
- Querying Microsoft SQL Server 2012 读书笔记:查询和管理XML数据 1 -使用FOR XML返回XML结果集
XML 介绍 <CustomersOrders> <Customer custid="1" companyname="Customer NRZBB&qu ...
- SQL Server ->> XML方法
1. 得到XML类型中某个节点下子节点的数量 DECLARE @xml xml SET @xml = ' <Parameters> <Parameter name = "p ...
- SQL SERVER中XML查询:FOR XML指定PATH
SQL SERVER中XML查询:FOR XML指定PATH 前言 在SQL SERVER中,XML查询能够指定RAW,AUTO,EXPLICIT,PATH.本文用一些实例介绍SQL SERVER中指 ...
随机推荐
- HDU-5025 Saving Tang Monk 广度搜索 状态压缩
题目链接:https://cn.vjudge.net/problem/HDU-5025 题意 救唐僧,路上有m(<=9)把钥匙,最多5条蛇和一个唐僧. 目标是前往唐僧的地方,用全部钥匙打开全部的 ...
- LightOJ-1336 Sigma Function 唯一分解定理 巧妙使用sqrt()等算数目
题目链接:https://cn.vjudge.net/problem/LightOJ-1336 题意 给出一个区间[1, n],求区间内所有数中因数之和为偶数的数目 思路 第二次写这个题 首先想到唯一 ...
- UVA-1602 Lattice Animals 搜索问题(打表+set)
题目链接 https://vjudge.net/problem/UVA-1602 紫书的一道例题,跟之前的很多题目有很多不同. 本题不像是一般的dfs或bfs这样的搜索套路,而是另一种枚举思路. 题意 ...
- java 模拟ajax上传图片
1.maven 引入依赖 <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpmime --> &l ...
- 获取mapper
static UpdateLogMapper updateLogMapper = (UpdateLogMapper)SpringContextUtil.getBean(UpdateLogMapper. ...
- Linux 磁盘管理及分区
硬盘结构和基础知识 扇区(Sector)为最小的物理储存单位,每个扇区为512 bytes,将扇区组成一个圆就是磁道(track),不同磁盘的相同磁道组成磁柱(Cylinder),磁柱是分区(par ...
- [PYTHON]一个简单的单元測试框架
近期尝试了一下TDD(測试驱动)的模式.感觉效果不错.在此总结一下,同学们假设有更好的办法,一定要告诉我:) 1. 每一个功能模块(文件),配一个单元測试模块. 以手头这个项目为样例:有LogCat. ...
- Qt5的插件机制(1)--Qt 框架中的插件载入机制概述
概述 Qt的源代码中通过 Q<pluginType>Factory.Q<pluginType>Plugin 和 Q<pluginType> 这三个类实现了Qt的插件 ...
- 多线程02---pThread简单介绍
1.简单介绍 pthread 是属于 POSIX 多线程开发框架. 它是c语言提供的一个跨平台的多线程解决方式.因为其在iOS编程中,操作比較麻烦.一般不用,这里介绍只作为了解. 2.pthread的 ...
- Android体验高扩展艺术般的适配器
前言 本篇文章带大家体验一下一种具有扩展性的适配器写法. 这个适配器主要用于Item有多种的情况下.当然仅仅有一种类型也是适用的 实现 毫无疑问我们要继承BaseAdapter,重写getCount, ...