以SQL Server格式读取xml数据

时间:2018-03-26 07:48:22

标签: sql sql-server xml sql-server-2008

我有这样的XML数据:

 <DOProposal xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <EndorsementNo />
      <SubmittedDate>2018-03-26T14:01:06.857</SubmittedDate>
      <LookupDate>2018-03-26T14:01:06.857</LookupDate>
      <LastSavedDate>2018-03-26T15:05:01.5687085+08:00</LastSavedDate>
      <SavedBy>5584</SavedBy>
      <ProposalId>f1fdc18d-25ea-4b03-a164-a8af00e704d8</ProposalId>
      <ProductID>ba484bb6-0a71-4941-98d4-63ced80a9a50</ProductID>
      <ProposalType>Proposal</ProposalType>
      <ProposalStatus>Referred</ProposalStatus>
      <UnderwritingMessages>
        <anyType xsi:type="xsd:string">305005:設立24ヶ月未満の会社は、への照会が必要です。</anyType>
        <anyType xsi:type="xsd:string">305008:ご照会内容がに照会されます。</anyType>
      </UnderwritingMessages>
      <CalculationMessages />
      <InitialQuoteDate>0001-01-01T00:00:00</InitialQuoteDate>
      <QuoteDate>2018-03-26 12:00:00 AM</QuoteDate>
      <QuoteExpiryDate>2018-09-22 11:59:00 PM</QuoteExpiryDate>
      <PolicyEffectiveDateByUnderwriter>0001-01-01 12:00:00 AM</PolicyEffectiveDateByUnderwriter>
      <PolicyCreatedDate>2018-03-26 02:01:06 PM</PolicyCreatedDate>
      <PolicyEffectiveDate />
      <PolicyExpiryDate>0001-01-01 12:00:00 AM</PolicyExpiryDate>
      <EndorsementEffectiveDate>0001-01-01T00:00:00</EndorsementEffectiveDate>
      <CancellationNotificationDate>0001-01-01T00:00:00</CancellationNotificationDate>
      <CancellationEffectiveDate>0001-01-01T00:00:00</CancellationEffectiveDate>
      <CancellationType>None</CancellationType>
      <PolicyNumber />
      <XmlVersion>1.0.2</XmlVersion>
      <BrokerID>2626</BrokerID>
      <BrokerUserID>5584</BrokerUserID>
      <UnderwriterID>3</UnderwriterID>
      <UnderwriterUserId>7975</UnderwriterUserId>
      <ClientID>0</ClientID>
      <InsuredNameCode />
      <AddressCode />
      <ExportStatusId>None</ExportStatusId>
      <UnderwriterComments />
      <Endorsements />
      <QuoteNumber>810DO 001859-9</QuoteNumber>
      <IsRenewal>false</IsRenewal>
      <IsEndorsement>false</IsEndorsement>
      <IsCancellation>false</IsCancellation>
      <TransType>NEW</TransType>

我使用以下格式来获取以下值。

select distinct P.proposalid as 'ProposalId',
p.Data.value('(/*/QuoteNumber)[1]', 'varchar(32)') PolicyNum,
p.data.value('(/*/TransType)[1]', 'nvarchar(max)') as TransType,
p.data.value('(/*/UnderwriterUserId)[1]', 'nvarchar(max)') as UW_Id,
u.UserLogin as UW_Login,
ps.proposalstatusdescription as 'Proposal Status',
isnull(Data.value('(/*/UWApprovalNumber)[1]', 'nvarchar(max)'),'') ApprovalNumber

from proposal p

-- referred before
cross APPLY (
    SELECT TOP 1 ProposalId, Starteffectivedate
     FROM Proposal
    WHERE ProposalId = P.ProposalId
    AND ProposalTypeID = 1
    AND ProposalStatusID = 3
    AND EndEffectiveDate <> '21991231'
    --and  len(isnull(Data.value('(/*/Answers/AnswersList/Entry[@key="uc7_UWApprovalNum"]/value)[1]', 'nvarchar(max)'),'') ) > 0
) P2
inner join userdetails u on (u.userid = p.data.value('(/*/UnderwriterUserId)[1]', 'nvarchar(max)'))
inner join proposalstatus ps on (ps.ProposalStatusID = p.ProposalStatusID)
where p.productid = 'DE23CFD5-E97A-4FCB-895A-A05E804C2EB5'
and p.starteffectivedate >= p2.starteffectivedate
and 
(p.proposalstatusid = '4' -- declined
or (p.proposalstatusid = '1' --and  len(isnull(Data.value('(/*/UWApprovalNumber)[1]', 'nvarchar(max)'),'')) > 0
)
)
and p.[starteffectivedate]>= '2017-01-01 00:00:00' and p.[starteffectivedate] <= '2017-12-31 23:59:59'
order by policynum

输出:

enter image description here

有了这个陈述,如果我有多于1个语句如何获取数据,我就能得到第一个:

我必须阅读UnderwritingMessages标记以作为每个提案的字符串返回,请帮助提出我需要如何获取数据的建议

2 个答案:

答案 0 :(得分:2)

您可以使用以下语法:

DECLARE @company table(data xml);

INSERT INTO @company VALUES
(
'<UnderwritingMessages>
    <anyType>1111</anyType>
    <anyType>1233</anyType>
 </UnderwritingMessages>
 <UnderwritingMessages>
    <anyType>2222</anyType>
    <anyType>3333</anyType>
 </UnderwritingMessages>'
);

 SELECT t.um.value('(.)[1]', 'nvarchar(100)') 
 FROM   @company
 CROSS APPLY data.nodes('UnderwritingMessages/anyType') as t(um);

----
1111
1233
2222
3333

答案 1 :(得分:1)

我根据您的代码准备了以下示例

/*
declare @xml xml = '
<UnderwritingMessages>
    <anyType type="xsd:string">1111</anyType>
    <anyType type="xsd:string">1233</anyType>
</UnderwritingMessages>
'
create table CompanyDetails(
    UserId int,
    XMLData xml
)
insert into CompanyDetails
select 1,
N'<UnderwritingMessages>
    <anyType type="xsd:string">1111</anyType>
    <anyType type="xsd:string">1233</anyType>
</UnderwritingMessages>'
*/

select 
    distinct P.UserId as 'UserId',
    [data].value('(/UnderwritingMessages)[1]', 'nvarchar(max)') as ApprovalNumber,
    [data].value('(/UnderwritingMessages/anyType)[1]', 'nvarchar(max)') as UnderWriterMessages 
from CompanyDetails p
CROSS APPLY XMLData.nodes('/') as root([data]) 

输出如下

enter image description here

您可以在给定文档中使用CROSS APPLY选项引用SQL XML查询