提问人:Marc L 提问时间:10/31/2022 最后编辑:Marc L 更新时间:10/31/2022 访问量:27
如何提取存储为具有复杂格式 sql 的表列中的数据的 xml 数据
How do i extract xml data stored as data in a table column with a complex formed sql
问:
我有来自第三方的XML数据(所以我无法更改XML格式)并存储在表格中。
XML 消息格式示例
<MedicalAidMessage xmlns="test.co.za/messaging" version="6.0">
<BenefitCheckResponseMessage>
<FinancialResponseDetails>
<FinancialResponseLines>
<FinancialResponseLine>
<LineIdentifier>1</LineIdentifier>
<RequestedAmount>1000</RequestedAmount>
<AmountToProvider>800</AmountToProvider>
<AmountToMember>0</AmountToMember>
<MemberLiableAmount>200</MemberLiableAmount>
<MemberNotLiableAmount>0</MemberNotLiableAmount>
<TariffCode>12345</TariffCode>
<LineResponseCodes>
<LineResponseCode>
<Sequence>1</Sequence>
<Code>274</Code>
<Description>We have not paid the amount claimed because the funds in the Medical Savings are used up.</Description>
<Type>Info</Type>
</LineResponseCode>
<LineResponseCode>
<Sequence>2</Sequence>
<Code>1239</Code>
<Description>We have applied a co-payment on this claim, in line with the member’s plan benefit for MRI and CT scans.</Description>
<Type>Info</Type>
</LineResponseCode>
</LineResponseCodes>
</FinancialResponseLine>
</FinancialResponseLines>
</FinancialResponseDetails>
</BenefitCheckResponseMessage>
</MedicalAidMessage>
我尝试了以下代码来获取 Tariffcode、RequestedAmount、AmountToProvider 和 AmountToMember
DECLARE @XMLData XML = (select top 1 replace(br.xmlmessage,'<?xml version="1.0" encoding="UTF-8"?>','')
from benefitcheckresponse br
inner join benefitcheck bc on bc.id = br.BenefitCheckId
where bc.id =1562
按 bc.id DESC订购)
SELECT
[TariffCode] = Node.Data.value('TariffCode', 'vacrhar(50)'),
[RequestedAmount] = Node.Data.value('RequestedAmount', 'float)'),
[AmountToProvider] = Node.Data.value('AmountToProvider', 'float)'),
[AmountToMember] = Node.Data.value('AmountToMember', 'float)')
FROM @XMLData.nodes('/*/FinancialResponseLine/') Node(Data)
我遇到的问题是它给了我以下错误消息
消息 9341,级别 16,状态 1,第 12 行 XQuery [nodes()]:语法错误 在 '' 附近,应为步骤表达式。
如何解决该错误?
当有多行响应时,如何包含多行的结果?
如何将子节点的值包含到线路响应节点?
答:
0赞
Thom A
10/31/2022
#1
如果我说实话,你最好使用这样的语法。我无法为您完成该条款,因为我不知道该按哪一列排序,但因为您有一个,您需要一个。您还需要定义您的 XML 命名空间,但您还没有定义,这将导致不返回任何行:ORDER BY
TOP (1)
WITH XMLNAMESPACES(DEFAULT 'test.co.za/messaging')
SELECT TOP (1)
FRL.FRL.value('(TariffCode/text())[1]','int') AS TariffCode,
FRL.FRL.value('(RequestedAmount/text())[1]','int') AS RequestedAmount, --I doubt float is the correct data type here
FRL.FRL.value('(AmountToProvider/text())[1]','int') AS AmountToProvider, --I doubt float is the correct data type here
FRL.FRL.value('(AmountToMember/text())[1]','int') AS AmountToMember --I doubt float is the correct data type here
FROM dbo.benefitcheckresponse br
INNER JOIN benefitcheck bc ON bc.id = br.BenefitCheckId
CROSS APPLY br.xmlmessage.nodes('MedicalAidMessage/BenefitCheckResponseMessage/FinancialResponseDetails/FinancialResponseLines/FinancialResponseLine') FRL(FRL)
WHERE bc.id = 1562
ORDER BY {Column Name(s)};
评论
0赞
Marc L
10/31/2022
这不起作用,因为 bc 列被定义为 varchar,而不是 XML,我无法更改定义。至于 order by,它用于 bc 表,所以 by bc.id desc
0赞
Thom A
10/31/2022
“BC 列定义为 varchar”你的问题没有说明,@MarcL;你只是告诉我们它是XML,因此我们为什么要假设你没有使用数据类型?因此,您需要 / 的值 ,可能使用 表结构。xml
CAST
CONVERT
xml
VALUES
0赞
Marc L
10/31/2022
正如您在原始代码中看到的,我将该列保存到 XML 变量中。这就是我这样做的原因,将保存的 Varchar 更改为 xml
评论