Sql server 对XML列的查询
我有一个SQL Server 2012表,其中一列是XML数据类型 以下是其中一个值:Sql server 对XML列的查询,sql-server,xml,Sql Server,Xml,我有一个SQL Server 2012表,其中一列是XML数据类型 以下是其中一个值: <items> <Counter CounterName="processed" CounterValue="70" /> <Counter CounterName="deferred" CounterValue="1" /> <Counter CounterName="delivered" CounterValue="70" /> <Co
<items>
<Counter CounterName="processed" CounterValue="70" />
<Counter CounterName="deferred" CounterValue="1" />
<Counter CounterName="delivered" CounterValue="70" />
<Counter CounterName="sent" CounterValue="70" />
<Counter CounterName="click" CounterValue="2" />
<Counter CounterName="open" CounterValue="22" />
</items>
这个怎么样:
DECLARE @input TABLE (ID INT NOT NULL, XmlCol XML)
INSERT INTO @input VALUES(1, '<items>
<Counter CounterName="processed" CounterValue="70" />
<Counter CounterName="deferred" CounterValue="1" />
<Counter CounterName="delivered" CounterValue="70" />
<Counter CounterName="sent" CounterValue="70" />
<Counter CounterName="click" CounterValue="2" />
<Counter CounterName="open" CounterValue="22" />
</items>'), (2, '<items>
<Counter CounterName="processed" CounterValue="170" />
<Counter CounterName="deferred" CounterValue="11" />
<Counter CounterName="delivered" CounterValue="170" />
<Counter CounterName="sent" CounterValue="170" />
<Counter CounterName="click" CounterValue="12" />
<Counter CounterName="open" CounterValue="212" />
</items>')
SELECT
ID,
Processed = xc.value('(Counter[@CounterName="processed"]/@CounterValue)[1]', 'int'),
Deferred = xc.value('(Counter[@CounterName="deferred"]/@CounterValue)[1]', 'int'),
Delivered = xc.value('(Counter[@CounterName="delivered"]/@CounterValue)[1]', 'int'),
[Sent] = xc.value('(Counter[@CounterName="sent"]/@CounterValue)[1]', 'int'),
Click = xc.value('(Counter[@CounterName="click"]/@CounterValue)[1]', 'int'),
[Open] = xc.value('(Counter[@CounterName="open"]/@CounterValue)[1]', 'int')
FROM
@input
CROSS APPLY
XmlCol.nodes('/items') AS XT(XC)
DECLARE@input表(ID INT不为NULL,XmlCol XML)
插入@input VALUES(1,'
'), (2, '
')
挑选
身份证件
Processed=xc.value('(计数器[@CounterName=“Processed”]/@CounterValue)[1],'int'),
Deferred=xc.value('(计数器[@CounterName=“Deferred”]/@CounterValue)[1],'int'),
Delivered=xc.value('(计数器[@CounterName=“Delivered”]/@CounterValue)[1],'int'),
[Sent]=xc.value('(计数器[@CounterName=“Sent”]/@CounterValue)[1],'int'),
单击=xc.value('(计数器[@CounterName=“单击”]/@CounterValue)[1],'int'),
[Open]=xc.value('(计数器[@CounterName=“Open”]/@CounterValue)[1],'int')
从…起
@输入
交叉应用
XmlCol.nodes('/items')作为XT(XC)
给我一个输出:
这篇文章深入讨论了对XML的查询,您能更具体一点吗?我对“这里有一个链接,去寻找答案”的解决方案不是很在行。看看如何学习钓鱼。宾果班戈。谢谢
DECLARE @input TABLE (ID INT NOT NULL, XmlCol XML)
INSERT INTO @input VALUES(1, '<items>
<Counter CounterName="processed" CounterValue="70" />
<Counter CounterName="deferred" CounterValue="1" />
<Counter CounterName="delivered" CounterValue="70" />
<Counter CounterName="sent" CounterValue="70" />
<Counter CounterName="click" CounterValue="2" />
<Counter CounterName="open" CounterValue="22" />
</items>'), (2, '<items>
<Counter CounterName="processed" CounterValue="170" />
<Counter CounterName="deferred" CounterValue="11" />
<Counter CounterName="delivered" CounterValue="170" />
<Counter CounterName="sent" CounterValue="170" />
<Counter CounterName="click" CounterValue="12" />
<Counter CounterName="open" CounterValue="212" />
</items>')
SELECT
ID,
Processed = xc.value('(Counter[@CounterName="processed"]/@CounterValue)[1]', 'int'),
Deferred = xc.value('(Counter[@CounterName="deferred"]/@CounterValue)[1]', 'int'),
Delivered = xc.value('(Counter[@CounterName="delivered"]/@CounterValue)[1]', 'int'),
[Sent] = xc.value('(Counter[@CounterName="sent"]/@CounterValue)[1]', 'int'),
Click = xc.value('(Counter[@CounterName="click"]/@CounterValue)[1]', 'int'),
[Open] = xc.value('(Counter[@CounterName="open"]/@CounterValue)[1]', 'int')
FROM
@input
CROSS APPLY
XmlCol.nodes('/items') AS XT(XC)