从SQL Server 2008中的xml中获取一些值

时间:2015-07-13 10:05:48

标签: sql xml sql-server-2008

我希望从Created元素下的xml中获取值RecordLocator<TicketHeader>),但我不能

我的xml是:

<Main>
  <Export>
    <Date>2015-07-06</Date>
    <Time>11:57:39</Time>
    <FromDate>2015-07-06</FromDate>
    <ToDate>2015-07-06</ToDate>
  </Export>
  <Tickets>
    <Ticket No="831-9275972480" Status="Issued" Type="Electron" SignInBooking="3OSWS" SignInTicketing="72" ReissueNo="">
      <TicketHeader>
        <History Ticketing="2015-07-06" Void="" Refund="" Reissue="" />
        <Created>2015-07-06 11:29:00</Created>
        <Modified />
        <ModifiedBy />
        <ClientCompanyName />
        <ClientNumber />
        <DistrubutorName />
        <DistributorNumber />
        <RecordLocator>RK3HDW</RecordLocator>
        <Subagent />
        <Interline>N</Interline>
        <TicketStatus>OK</TicketStatus>
        <TKTcoupons />
        <GlobalCouponStatus />
        <TermID>BDB7F9</TermID>
        <MIRdev>FED02B</MIRdev>
        <TransBeforeTKT>016</TransBeforeTKT>
        <AutoPrice>N</AutoPrice>
        <BookedDirectly>N</BookedDirectly>
        <FrequentFlyer />
        <FareCalc>ZAG OU DBV 92.00OU ZAG 92.00 HRK184.00END XT 125.00ZS110.00YQ</FareCalc>
        <Routing>ZAG-DBV-ZAG</Routing>
        <ItineraryIndicator>D</ItineraryIndicator>
        <PNRCreationDate>2015-07-03</PNRCreationDate>
        <OfficeID Booking="3OS " TR="" Ticketing="3OS " />
        <Contacts>ZAGN*385915885134__ZAGT*00385 1 6329-100 AVIOKARTE.HR-IP__ZAGE*AVIOKARTE//AVIOKARTE.HR</Contacts>
        <TktAgentTitle />
        <TktOfficeTitle>3OS</TktOfficeTitle>
        <BookingOfficeTitle>3OS</BookingOfficeTitle>
        <IATA>75321164</IATA>
        <ValidAirlineName>CROATIA AIRLINES</ValidAirlineName>

我的sql代码就像tihs:

CREATE TABLE _GoLink_Xml
(
      RecordId INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
      XmlData XML NOT NULL
)
GO



INSERT INTO _GoLink_Xml(XmlData)
SELECT *
FROM OPENROWSET(
   BULK 'C:\golink_0607.xml', SINGLE_BLOB)
AS ImportSource
GO




SELECT 
   ExportDate = ColExport.value('(Date)[1]', 'date') ,
   ExportTime = ColExport.value('(Time)[1]', 'time'),
   ExportFromDate = ColExport.value('(FromDate)[1]', 'date'),
   ExportToDate = ColExport.value('(ToDate)[1]', 'date'),
   -- use the @Status to get the XML attribute (not element!) for "Status"
   TicketNo=ColTicketNo.value('@No','varchar(50)'),
   TicketStatus = ColTicketStatus.value('@Status','varchar(50)') ,
   TicketType = ColTicketType.value('@Type','varchar(50)') ,
   TicketSignInBooking = ColTicketSignInBooking.value('@SignInBooking','varchar(6)'),
   TicketReissueNo = ColTicketReissueNo.value('@ReissueNo','varchar(6)'),
   TicketHeaderTicketing = ColHeaderTicketing.value('@Ticketing','date'),
   TicketHeaderVoid = ColHeaderTicktVoid.value('@Void','varchar(50)'),
   TicketHeaderCreated = ColHeaderCreated.value('(Created/text())[1]','datetime'),
   TicketHeaderRecordLocator = ColHeaderRecordLocator.value('@RecordLocator','varchar(6)'),
   TicketHeaderBooking = ColHeaderTicktBooking.value('@Booking','varchar(50)'),
   TicketHeaderTicketing = ColHeaderTicktTicketing.value('@Ticketing','varchar(50)'),
   TicketHeaderContacts = ColHeaderContacts.value('(Contacts)[1]','varchar(max)'),
   Currency = Currency.value('@Currency','varchar(5)'),
   Value = Value.value('@Value','decimal(18,2)'),
   RateHRK = RateHrk.value('(Rate)[1]','decimal(18,2)')



  -- TicketHeaderRecordLocator = ColHeaderRecordLocator.value('(RecordLocator)[1]','varchar(6)')         
FROM 
   _GoLink_Xml
    CROSS APPLY
   -- the nodes <Main>/<Export> have some base data
   XmlData.nodes('Main/Export') AS XT1(ColExport)
    CROSS APPLY
   -- the nodes <Main>/<Tickets>/<Ticket> have ticket-related data
   XmlData.nodes('Main/Tickets/Ticket') AS XT2(ColTicketStatus)
   CROSS APPLY
   XmlData.nodes('Main/Tickets/Ticket') AS XT3(ColTicketNo)
   CROSS APPLY
   XmlData.nodes('Main/Tickets/Ticket') AS XT4(ColTicketType)
    CROSS APPLY
   XmlData.nodes('Main/Tickets/Ticket') AS XT5(ColTicketSignInBooking)
    CROSS APPLY
   XmlData.nodes('Main/Tickets/Ticket') AS XT6(ColTicketReissueNo)
    CROSS APPLY
   XmlData.nodes('Main/Tickets/Ticket/TicketHeader/History') AS XT7(ColHeaderTicketing)
   CROSS APPLY
    XmlData.nodes('Main/Tickets/Ticket/TicketHeader/History') AS XT8(ColHeaderTicktVoid)
    CROSS APPLY
    XmlData.nodes('Main/Tickets/Ticket/TicketHeader/Created') AS XT9(ColHeaderCreated)
    CROSS APPLY
    XmlData.nodes('Main/Tickets/Ticket/TicketHeader/RecordLocator') AS XT10(ColHeaderRecordLocator)
    CROSS APPLY
    XmlData.nodes('Main/Tickets/Ticket/TicketHeader/OfficeID') AS XT11(ColHeaderTicktBooking)
    CROSS APPLY
    XmlData.nodes('Main/Tickets/Ticket/TicketHeader/OfficeID') AS XT12(ColHeaderTicktTicketing)
    CROSS APPLY
    XmlData.nodes('Main/Tickets/Ticket/TicketHeader/Contacts') AS XT13(ColHeaderContacts)
       CROSS APPLY
    XmlData.nodes('Main/Tickets/Ticket/TicketHeader/Rate') AS XT14(Currency)
       CROSS APPLY
    XmlData.nodes('Main/Tickets/Ticket/TicketHeader/Rate') AS XT15(Value)
   CROSS APPLY
    XmlData.nodes('Main/Tickets/Ticket/TicketHeader/RateHRK') AS XT16(RateHrk)

但没有结果,任何人都可以帮忙解决这个问题吗?

由于

现在我读完整个XML,我的查询就像:

SELECT 
   ExportDate = ColExport.value('(Date)[1]', 'date') ,
   ExportTime = ColExport.value('(Time)[1]', 'char(10)'),
   ExportFromDate = ColExport.value('(FromDate)[1]', 'date'),
   ExportToDate = ColExport.value('(ToDate)[1]', 'date'),
   -- use the @Status to get the XML attribute (not element!) for "Status"
   TicketNo=ColTicketHeader.value('@No','varchar(50)'),
   TicketStatus = ColTicketHeader.value('@Status','varchar(50)') ,
   TicketType = ColTicketHeader.value('@Type','varchar(50)') ,
   TicketSignInBooking = ColTicketHeader.value('@SignInBooking','varchar(6)'),
   TicketReissueNo = ColTicketHeader.value('@ReissueNo','varchar(6)'),
   TicketHeaderTicketing = ColHisotry.value('@Ticketing','date'),
   TicketHeaderVoid = ColHisotry.value('@Void','varchar(50)'),
   TicketHeaderCreated = ColTicketHeaderr.value('(Created)[1]','datetime'),
   TicketHeaderRecordLocator = ColTicketHeaderr.value('(RecordLocator)[1]', 'varchar(25)'),
   TicketStatus = ColTicketHeaderr.value('(TicketStatus)[1]', 'varchar(25)'),
   TermiID = ColTicketHeaderr.value('(TermID)[1]', 'varchar(25)'),
   PNRCreationDate = ColTicketHeaderr.value('(PNRCreationDate)[1]', 'date'),
   BookingOffice = ColOfficeID.value('@Booking','varchar(50)'),
   TicketingOffice = ColOfficeID.value('@Ticketing','varchar(50)'),
   Routing = ColTicketHeaderr.value('(Routing)[1]', 'varchar(100)'),
   AirlineName = ColTicketHeaderr.value('(ValidAirlineName)[1]', 'varchar(100)'),
   AirlineCode = ColTicketHeaderr.value('(ValidatingCarrier)[1]', 'varchar(5)'),
   Currency = ColRate.value('@Currency','varchar(5)'),
   Value = ColRate.value('@Value','decimal(18,2)'),
   RateHRK = ColTicketHeaderr.value('(RateHRK)[1]', 'decimal(18,2)'),
   ConvertationRate = ColTicketHeaderr.value('(ConvertationRate)[1]', 'decimal(18,4)'),
   Comission = ColTicketHeaderr.value('(Comission)[1]', 'varchar(50)'),
   FOP = ColTicketHeaderr.value('(FP)[1]', 'varchar(50)'),
   ServiceFee = ColSFee.value('@Main','decimal(18,2)'),
   Discount = ColTicketHeaderr.value('(Discount)[1]', 'decimal(18,2)'),
   AirlinePayTotal = ColTicketHeaderr.value('(AirlinePayTotal)[1]', 'varchar(20)'),
   TaxCurr = ColTax.value('@Curr','varchar(5)'),
   TaxValue = ColTax.value('@Value','decimal(18,2)'),
   OrderNo= ColPax.value('@No','int'),
   OrderLastName= ColPax.value('@LastName','varchar(100)'),
   OrderFirstName= ColPax.value('@FirstName','varchar(100)'),
   [No]= ColAirSegment.value('@No','int'),
   BookingClass= ColAirSegment.value('@Class','varchar(5)'),
   FlightNO = ColAirSegment.value('(FlightNo)[1]', 'varchar(20)'),
   FlightDurationTime = ColAirSegment.value('(FlightDurationTime)[1]', 'varchar(20)'),
   GeographicalMileage = ColAirSegment.value('(GeographicalMileage)[1]', 'varchar(20)'),
   Meal = ColAirSegment.value('(Meal)[1]', 'varchar(5)'),
   DepartureDate = ColDeparture.value('@Date','date'),
   DepartureTime = ColDeparture.value('@Time','char(10)'),
   ArrivalDate = ColArrival.value('@Date','date'),
   ArrivalTime = ColArrival.value('@Time','char(10)'),
   BoardCity = ColBoard.value('@City','varchar(50)'),
   BoardCityCode = ColBoard.value('@Point','varchar(50)'),
   OffCity = ColOff.value('@City','varchar(50)'),
   OffCityCode = ColOff.value('@Point','varchar(50)'),
   BaggageAllowance= ColAirSegment.value('(BaggageAllowance)[1]', 'varchar(20)')

FROM 
   _GoLink_Xml
    CROSS APPLY

   XmlData.nodes('Main/Export') AS XT1(ColExport)
    CROSS APPLY

    XmlData.nodes('Main/Tickets/Ticket') AS XT2(ColTicketHeader)
        CROSS APPLY
    XmlData.nodes('Main/Tickets/Ticket/TicketHeader/History') AS XT3(ColHisotry)
         CROSS APPLY
    XmlData.nodes('Main/Tickets/Ticket/TicketHeader') AS XT4(ColTicketHeaderr)
     CROSS APPLY
    XmlData.nodes('Main/Tickets/Ticket/TicketHeader/OfficeID') AS XT5(ColOfficeID)
      CROSS APPLY
    XmlData.nodes('Main/Tickets/Ticket/TicketHeader/Rate') AS XT6(ColRate)
      CROSS APPLY
    XmlData.nodes('Main/Tickets/Ticket/TicketHeader/ServiceFee') AS XT7(ColSFee)
      CROSS APPLY
    XmlData.nodes('Main/Tickets/Ticket/TicketHeader/Taxes') AS XT8(ColTax)
      CROSS APPLY
    XmlData.nodes('Main/Tickets/Ticket/NE') AS XT9(ColPax)
         CROSS APPLY
    XmlData.nodes('Main/Tickets/Ticket/AirSegments/AirSegment') AS XT10(ColAirSegment)
        CROSS APPLY
    XmlData.nodes('Main/Tickets/Ticket/AirSegments/AirSegment/Departure') AS XT11(ColDeparture)
        CROSS APPLY
    XmlData.nodes('Main/Tickets/Ticket/AirSegments/AirSegment/Arrival') AS XT12(ColArrival)
           CROSS APPLY
    XmlData.nodes('Main/Tickets/Ticket/AirSegments/AirSegment/Board') AS XT13(ColBoard)
         CROSS APPLY
    XmlData.nodes('Main/Tickets/Ticket/AirSegments/AirSegment/Off') AS XT14(ColOff)

for XML

<Main>
  <Export>
    <Date>2015-07-06</Date>
    <Time>11:57:39</Time>
    <FromDate>2015-07-06</FromDate>
    <ToDate>2015-07-06</ToDate>
  </Export>
  <Tickets>
    <Ticket No="831-9275972480" Status="Issued" Type="Electron" SignInBooking="3OSWS" SignInTicketing="72" ReissueNo="">
      <TicketHeader>
        <History Ticketing="2015-07-06" Void="" Refund="" Reissue="" />
        <Created>2015-07-06 11:29:00</Created>
        <Modified />
        <ModifiedBy />
        <ClientCompanyName />
        <ClientNumber />
        <DistrubutorName />
        <DistributorNumber />
        <RecordLocator>RK3HDW</RecordLocator>
        <Subagent />
        <Interline>N</Interline>
        <TicketStatus>OK</TicketStatus>
        <TKTcoupons />
        <GlobalCouponStatus />
        <TermID>BDB7F9</TermID>
        <MIRdev>FED02B</MIRdev>
        <TransBeforeTKT>016</TransBeforeTKT>
        <AutoPrice>N</AutoPrice>
        <BookedDirectly>N</BookedDirectly>
        <FrequentFlyer />
        <FareCalc>ZAG OU DBV 92.00OU ZAG 92.00 HRK184.00END XT 125.00ZS110.00YQ</FareCalc>
        <Routing>ZAG-DBV-ZAG</Routing>
        <ItineraryIndicator>D</ItineraryIndicator>
        <PNRCreationDate>2015-07-03</PNRCreationDate>
        <OfficeID Booking="3OS " TR="" Ticketing="3OS " />
        <Contacts>ZAGN*385915885134__ZAGT*00385 1 6329-100 AVIOKARTE.HR-IP__ZAGE*AVIOKARTE//AVIOKARTE.HR</Contacts>
        <TktAgentTitle />
        <TktOfficeTitle>3OS</TktOfficeTitle>
        <BookingOfficeTitle>3OS</BookingOfficeTitle>
        <IATA>75321164</IATA>
        <ValidAirlineName>CROATIA AIRLINES</ValidAirlineName>
        <ValidatingCarrier>OU</ValidatingCarrier>
        <Rate Currency="HRK" Value="184.00" />
        <RateHRK>0.00</RateHRK>
        <ConvertationRate>0.0000</ConvertationRate>
        <TourCode />
        <Comission>0.00A</Comission>
        <CommisAmt Curr="HRK" Value="0.00" />
        <FP>INVOICE</FP>
        <CreditCard />
        <AirRecLoc>1A-8FDQTV</AirRecLoc>
        <Endorsement>OU ONLY/ /REFUND NOT PERMITTED/ </Endorsement>
        <Amount>635.00</Amount>
        <ServiceFee Main="0.00" VAT="0.00" />
        <Discount>0.00</Discount>
        <ClientToPay>0.00</ClientToPay>
        <AirlinePayTotal>635,00</AirlinePayTotal>
        <NetEarnedExclTaxes>0,00</NetEarnedExclTaxes>
        <AgentCountry>HR </AgentCountry>
        <Remarks>from

          <Remark />
          <Remark1 />
          <Remark2>DI.FA2: DI.FA3: IGOR DI.FA4: JURISIC DI.FA5: OGRIZOVICEVA 34 DI.FA6: 10000 DI.FA7: ZAGREB DI.FA8: HR DI.FA9: BT DI.FA10: 03072015 DI.FA13: 385915885134 DI.FA20: DI.FA21: HRK 368 902 40 DI.FA30: UHR </Remark2>
          <Remark3 />
        </Remarks>
        <Taxes Curr="HRK" Value="451.00">
          <Tax Curr="HRK" Value="206.00" TC="HR" NC="" />
          <Tax Curr="HRK" Value="10.00" TC="MI" NC="" />
          <Tax Curr="HRK" Value="110.00" TC="YQ" NC="" />
          <Tax Curr="HRK" Value="125.00" TC="ZS" NC="" />
        </Taxes>
        <CarrierTKTFees />
      </TicketHeader>
      <NE No="1" LastName="JURISIC" FirstName="IGORMR" Title="" text="ADT" />
      <AirSegments>
        <AirSegment No="01" Class="U">
          <ServicingCarrier>OU</ServicingCarrier>
          <ServicingCarrierName>CROATIA AIRLINES</ServicingCarrierName>
          <OpratingCarrierName />
          <FlightNo>660</FlightNo>
          <SeatNumber />
          <FlightDurationTime>0055</FlightDurationTime>
          <GeographicalMileage>00245</GeographicalMileage>
          <Meal>N</Meal>
          <MealSSR />
          <AirClass>U</AirClass>
          <Departure Date="2015-10-02" Time="05:50:00" />
          <Arrival Date="2015-10-02" Time="06:45:00" />
          <Board City="ZAGREB" Point="ZAG" Terminal="" />
          <Off City="DUBROVNIK" Point="DBV" Terminal="" />
          <FareBasis>UPROM</FareBasis>
          <BaggageAllowance>1PC</BaggageAllowance>
          <Equipment>319</Equipment>
          <WCHR />
          <ACRecLoc />
        </AirSegment>
        <AirSegment No="02" Class="U">
          <ServicingCarrier>OU</ServicingCarrier>
          <ServicingCarrierName>CROATIA AIRLINES</ServicingCarrierName>
          <OpratingCarrierName />
          <FlightNo>669</FlightNo>
          <SeatNumber />
          <FlightDurationTime>0055</FlightDurationTime>
          <GeographicalMileage>00245</GeographicalMileage>
          <Meal>N</Meal>
          <MealSSR />
          <AirClass>U</AirClass>
          <Departure Date="2015-10-06" Time="21:20:00" />
          <Arrival Date="2015-10-06" Time="22:15:00" />
          <Board City="DUBROVNIK" Point="DBV" Terminal="" />
          <Off City="ZAGREB" Point="ZAG" Terminal="" />
          <FareBasis>UPROM</FareBasis>
          <BaggageAllowance>1PC</BaggageAllowance>
          <Equipment>319</Equipment>
          <WCHR />
          <ACRecLoc />
        </AirSegment>
      </AirSegments>
    </Ticket>
  </Tickets>
</Main>

我有32条记录,有些记录是多条记录,我需要加上一些不同的记录。 另外计划在附加表格中加入一些信息...... 也许你有答案? 谢谢

1 个答案:

答案 0 :(得分:2)

应该非常简单(如果你有有效的XML - 你发布的内容不是有效的。)

尝试这样的事情:

SELECT 
    ColTicketHeader.value('(RecordLocator)[1]', 'varchar(25)'),
    ColTicketHeader.value('(Created)[1]', 'datetime')
FROM 
   _GoLink_Xml 
CROSS APPLY
   XmlData.nodes('Main/Tickets/Ticket/TicketHeader') AS XT7(ColTicketHeader)

另外: 为什么在地球上 你是多次交叉应用相同的XPath?

CROSS APPLY
   -- the nodes <Main>/<Tickets>/<Ticket> have ticket-related data
   XmlData.nodes('Main/Tickets/Ticket') AS XT2(ColTicketStatus)
CROSS APPLY
   XmlData.nodes('Main/Tickets/Ticket') AS XT3(ColTicketNo)
CROSS APPLY
   XmlData.nodes('Main/Tickets/Ticket') AS XT4(ColTicketType)
CROSS APPLY
   XmlData.nodes('Main/Tickets/Ticket') AS XT5(ColTicketSignInBooking)
CROSS APPLY
   XmlData.nodes('Main/Tickets/Ticket') AS XT6(ColTicketReissueNo)

这使毫无意义 - 只需使用

CROSS APPLY
   XmlData.nodes('Main/Tickets/Ticket') AS XT2(ColTicket)

然后让<Ticket>所需的所有详细信息引用此单个XT2(ColTicket)伪表/列 - 将使您的T-SQL 更多更简单,更容易理解!