1

谁能帮我<QuoteId>使用 T-SQL 和 XQuery 从附加的 XML 文件中检索一个值?

我正在尝试使用这个 T-SQL 但没有运气

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) 

这是我用来检索QuoteId

DECLARE @QuoteResponse XML

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

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

谢谢

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

2 回答 2

2

这个怎么样:

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

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

您只需要遵循 XML 元素层次结构 - 直到您读取所需的值:

<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 !
于 2012-08-28T14:28:58.477 回答
0

您是否尝试过创建临时表然后使用 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)
于 2012-08-28T16:28:28.787 回答