Recuperar un valor de XML usando T-SQL

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

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

Muchas Gracias

<GetQuoteResponse xmlns:xsi="" xmlns:xsd="">
          <PromotionCode />
          <Traveller requestedMedicalScreening="false">
            <FirstName>xxx </FirstName>
      <SummaryOfCover />

preguntado el 28 de agosto de 12 a las 15:08

2 Respuestas

Qué tal esto:

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

    @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 ....
  <OperationResult>  -- irrelvant - not in here.....
  <QuoteResults>       -- this one is relevant ....
    <QuoteResult>      -- this one is relevant ....
      <OperationResult>  -- irrelvant - not in here..... 
      <MainData>   -- irrelvant - not in here..... 
      <Price>    -- irrelvant - not in here..... 
      <QuoteId>322423234</QuoteId>   -- here's our value !

Respondido 28 ago 12, 15:08

Still no luck. it is returning null value. - user740022

@user740022: then your XML must look different - this code works just fine when I run it. Can you check your XML again? - marc_s

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

@ usuario740022: see this SQL Fiddle - it reproduces your table and XML exactly - and if you run it, you conseguir the proper value from <QuoteId>. Allí debe ser something that's different in your database .... - marc_s

Thanks Marc. Yeah, my XML file was containing the default namespace and that was the problem. Now it works well. - user740022

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'

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)

Respondido 28 ago 12, 17:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.