[英]SQL Query Column with XML Data
I have the following data in a column, how can I query the values inside. 我在列中有以下数据,如何查询其中的值。
<deliveryPart notificationId="12345">
<message address="email@email.com" content="multipart/alternative" domain="email.com" format="0" id="159436637" recipient-id="098765" targetCode="__MAIN__">
<custom>
<recipient Nickname="mynickname" id="54321" />
<targetData Incident_Id="1509403" reference_nb="0000-0000" />
</custom>
</message>
</deliveryPart>
I gave it a quick go but I've never done it before and I am in short of time. 我快速浏览了一下,但之前从未做过,而且时间紧迫。
select top 10 *
from [db].[db].[table]
where mData.value('(deliveryPart/message/recipient-id)[1]','varchar(max)') like '098765'
I get the following error 我收到以下错误
Msg 4121, Level 16, State 1, Line 1
消息4121,第16级,状态1,第1行
Cannot find either column "mData" or the user-defined function or aggregate "mData.value", or the name is ambiguous.找不到列“ mData”或用户定义的函数或聚合“ mData.value”,或者名称不明确。
UPDATE UPDATE
I am using the following code to fetch the xml values and it works 我正在使用以下代码来获取xml值,并且可以正常工作
SELECT TOP 1000 B1.[mData].value('(deliveryPart/message/@id)[1]', 'NVARCHAR(MAX)') AS 'MessageID', B1.[mData].value('(deliveryPart/message/@address)[1]', 'NVARCHAR(MAX)') AS 'Address'
FROM (SELECT CAST(mData as XML) as xmlData FROM [dbo].[db].[table]) B0
CROSS APPLY xmlData.nodes('/') B1 (mData)
WHERE B1.[mData].value('(deliveryPart/message/@address)[1]', 'NVARCHAR(MAX)') LIKE '%@%'
And it returns the xml values store in the ntext field just fine. 并且它返回存储在ntext字段中的xml值就好了。
180646774 email1@email.com
159436627 test2@hotmail.com
159436637 test3@hotmail.com
However, I need to fetch values from outside the mData column and is not letting me do it. 但是,我需要从mData列外部获取值,而不是让我这样做。
You need to use the @recipient-id
as a XML attribute - not XML element: 您需要使用
@recipient-id
作为XML 属性 -而不是XML元素:
<message address="email@email.com" content="multipart/alternative"
domain="email.com" format="0" id="159436637"
recipient-id="098765" targetCode="__MAIN__">
************ this is an *attribute* - use the `@` to select it!
Code: 码:
select top 10 *
from [db].[db].[table]
where mData.value('(deliveryPart/message/@recipient-id)[1]', 'varchar(max)') like '098765'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.