1

Can anyone please help me to retrieve a value <QuoteId> from the attached XML file using T-SQL and XQuery?

I am trying to use this T-SQL but no luck

declare @QuoteResponse XML
select @QuoteResponse =gQuoteResponse FROM AutoRenew where policyid= '454544'

SELECT b.value('(OverallResultStatus)[1]', 'varchar(100)')  as status
FROM
@QuoteResponse.nodes('GetQuoteResponse/OperationResult') as a(b) 

This is I am using to retrieve the QuoteId

DECLARE @QuoteResponse XML

SELECT @QuoteResponse = gQuoteResponse FROM [dbo].[AutoRenew] WHERE policyid = '454544'

SELECT
    @QuoteResponse.value('(GetQuoteResponse/QuoteResults/QuoteResult/QuoteId)[1]', 'bigint')

Thanks

<GetQuoteResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <OperationResult>
    <OverallResultStatus>xxxx</OverallResultStatus>
    <Results>
      <Result>
        <ResultStatus>xxxx</ResultStatus>
        <ResultCode>ddf</ResultCode>
        <ResultMessage>xxxxx</ResultMessage>
      </Result>
    </Results>
  </OperationResult>
  <QuoteResults>
    <QuoteResult>
      <OperationResult>
        <OverallResultStatus>xxx</OverallResultStatus>
        <Results>
          <Result>
            <ResultStatus>xx</ResultStatus>
            <ResultCode>xxxx</ResultCode>
            <ResultMessage>xx</ResultMessage>
          </Result>
        </Results>
      </OperationResult>
      <MainData>
        <StartDate>2012-08-14T00:00:00</StartDate>
        <ReturnDate>2013-08-13T00:00:00</ReturnDate>
        <TripType>xxx</TripType>
        <Area>xxx</Area>
        <Relationship>Individual</Relationship>
        <Product>
          <ProductId>xxx</ProductId>
          <Name>xxxx</Name>
        </Product>
        <Endorsements>
          <Endorsement>
            <EndorsementCode>xx</EndorsementCode>
            <Selected>xx</Selected>
            <Name>xxx</Name>
          </Endorsement>
          <Endorsement>
            <EndorsementCode>xxx</EndorsementCode>
            <Selected>xxx</Selected>
            <Name>xxxx</Name>
          </Endorsement>
        </Endorsements>
        <Promotion>
          <PromotionCode />
        </Promotion>
        <Travellers>
          <Traveller requestedMedicalScreening="false">
            <TravellerId>xxxx</TravellerId>
            <Title>xxx</Title>
            <FirstName>xxx </FirstName>
            <LastName>xxxx</LastName>
            <Age>xxx</Age>
          </Traveller>
        </Travellers>
      </MainData>
      <Price>
        <NetToParentAgent>xxx</NetToParentAgent>
        <NetPrice>xxx</NetPrice>
        <Tax>edew</Tax>
        <GrossPriceWithAllSurchargesAndMedicalScreenings>103.02</GrossPriceWithAllSurchargesAndMedicalScreenings>
      </Price>
      <QuoteId>322423234</QuoteId>
      <QuoteExpiryDate>xxx</QuoteExpiryDate>
      <SummaryOfCover />
    </QuoteResult>
  </QuoteResults>
</GetQuoteResponse>

2 Answers 2

2

How about this:

DECLARE @QuoteResponse XML
SELECT @QuoteResponse = gQuoteResponse FROM dbo.AutoRenew WHERE policyid = '454544'

SELECT
    @QuoteResponse.value('(/GetQuoteResponse/QuoteResults/QuoteResult/QuoteId)[1]', 'bigint')

You just need to follow down the XML element hierarchy - until you read your desired value:

<GetQuoteResponse     -- this one is relevant ....
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <OperationResult>  -- irrelvant - not in here.....
    ......
  </OperationResult>
  <QuoteResults>       -- this one is relevant ....
    <QuoteResult>      -- this one is relevant ....
      <OperationResult>  -- irrelvant - not in here..... 
        ....
      </OperationResult>
      <MainData>   -- irrelvant - not in here..... 
        ......
      </MainData>
      <Price>    -- irrelvant - not in here..... 
        ......
      </Price>
      <QuoteId>322423234</QuoteId>   -- here's our value !
6
  • Still no luck. it is returning null value.
    – user740022
    Aug 28, 2012 at 15:36
  • @user740022: then your XML must look different - this code works just fine when I run it. Can you check your XML again?
    – marc_s
    Aug 28, 2012 at 15:45
  • Yes, I did check it . There are no change in the XML file. I am using the same XML file. But still returning null value. Your help would be much appreciated. Thanks.
    – user740022
    Aug 28, 2012 at 15:59
  • 1
    @user740022: see this SQL Fiddle - it reproduces your table and XML exactly - and if you run it, you do get the proper value from <QuoteId>. There must be something that's different in your database ....
    – marc_s
    Aug 28, 2012 at 16:12
  • 1
    Thanks Marc. Yeah, my XML file was containing the default namespace and that was the problem. Now it works well.
    – user740022
    Aug 29, 2012 at 8:15
0

Have you tried creating a temp table then populating the temp table using openxml?

DECLARE @QuoteResponse XML
SELECT @QuoteResponse =gQuoteResponse FROM AutoRenew where policyid= '454544'

DECLARE @idoc INT
EXEC sp_xml_preparedocument @idoc OUTPUT, @QuoteResponse

CREATE TABLE #QuoteIds(   
    [QuoteId] VARCHAR (10))

INSERT INTO #QuoteIds ([QuoteId])
    SELECT QuoteId
    FROM OPENXML(@idoc, '/QuoteResults/QuoteResult/QuoteId', 2) WITH #QuoteIds

DECLARE @quoteid VARCHAR (10)
SET @quoteid = (SELECT TOP 1 QuoteId FROM #QuoteIds)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.