Recuperar un valor de XML usando T-SQL
Frecuentes
Visto 381 veces
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')
Muchas Gracias
<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 Respuestas
2
Qué tal esto:
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 !
Respondido 28 ago 12, 15:08
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)
Respondido 28 ago 12, 17:08
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas sql-server xml tsql xquery or haz tu propia pregunta.
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_sThanks Marc. Yeah, my XML file was containing the default namespace and that was the problem. Now it works well. - user740022