我有以下XML和代码,可以撤回AccountNumber和Status。我还需要退回......
我不知道如何获取ID属性,我想知道......我可以在同一个查询中获取所有这些信息(来自多个节点)吗?
谢谢!
DECLARE @doc xml
SET @doc =
'<?xml version="1.0" encoding="UTF-8"?>
<p:OrderRequest xmlns:p="http://XXX.Schemas.OrderRequest/2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://XXX.Schemas.OrderRequest/2 OrderRequestV2.xsd ">
<p:Header>
<p:SCRARequestCount>0</p:SCRARequestCount>
<p:MFRRequestCount>1</p:MFRRequestCount>
<p:FileCreatedAt>2001-12-31T12:00:00</p:FileCreatedAt>
<p:RequestFileName>p:RequestFileName</p:RequestFileName>
</p:Header>
<p:RequestItems>
<p:MFRRequest id="1" priority="">
<p:AccountNumber>9999999</p:AccountNumber>
<Status>Initial</Status>
<p:CaseInformation>
<ReferralDate>2011-01-01</ReferralDate>
<LoanType>1A</LoanType>
<ARM>Yes</ARM>
<InvestorNumber>InvestorNumber</InvestorNumber>
<PropertyAddress>PropertyAddress</PropertyAddress>
<PrivateLabel>Yes</PrivateLabel>
<CaseNumber>01-11111/AK/</CaseNumber>
<SuspenseBalance>9999.00</SuspenseBalance>
<TitleOrderedDate>2011-09-01</TitleOrderedDate>
<TotalMonthlyPayment>876.99</TotalMonthlyPayment>
</p:CaseInformation>
</p:MFRRequest>
</p:RequestItems>
</p:OrderRequest>'
;WITH XMLNAMESPACES('http://XXX.Schemas.OrderRequest/2' AS p)
SELECT
Y.i.value('Status[1]', 'varchar(10)') AS Status ,
Y.i.value('p:AccountNumber[1]', 'varchar(10)') AS AccountNumber
FROM @doc.nodes('/p:OrderRequest/p:RequestItems/p:MFRRequest') AS Y(i)
答案 0 :(得分:2)
尝试这样的事情:
;WITH XMLNAMESPACES('http://XXX.Schemas.OrderRequest/2' AS p)
SELECT
MFRRequestID = Y.i.value('(@id)[1]', 'int'),
RequestStatus = Y.i.value('Status[1]', 'varchar(10)') ,
AccountNumber = Y.i.value('p:AccountNumber[1]', 'varchar(10)'),
ReferralDate = CIF.value('(ReferralDate)[1]', 'varchar(25)'),
CaseInfoType = CIF.value('(Type)[1]', 'varchar(25)')
FROM
@doc.nodes('/p:OrderRequest/p:RequestItems/p:MFRRequest') AS Y(i)
CROSS APPLY
i.nodes('p:CaseInformation') AS Tbl(CIF)
基本上,一旦你的<p:MFRRequest>
伪表中有Y(i)
XML元素,你就可以再次使用.nodes()
的第二次调用来获取(可能是多个){{ 1}}节点并在这个新的第二个伪表上使用<p:CaseInfomration>
调用从中提取单个信息位。