Kapil
Kapil

Reputation: 1931

Read xml data in format SQL Server

I have XML data like this:

 <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>

I am using the below format to fetch the following values.

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

Output:

enter image description here

With this statement, I am able to get the 1st one if I have more than 1 statements as above how to fetch the data:

I have to read the UnderwritingMessages tag to return as string for each proposal, kindly help to give the suggestion how I need to fetch the data

Upvotes: 0

Views: 62

Answers (2)

Eralper
Eralper

Reputation: 6622

I have prepared following sample based on your codes

/*
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]) 

Output will be as follows

enter image description here

You can refer to SQL XML queries with CROSS APPLY options at given document

Upvotes: 1

McNets
McNets

Reputation: 10827

You can use this syntax:

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

Upvotes: 2

Related Questions