DECLARE @idoc int
DECLARE @doc varchar(1000)
SET @doc ='
<ROOT>
<Customer CustomerID="VINET"  >
     <ContactName>Paul Henriot</ContactName>
   <Order OrderID="10248" CustomerID="VINET" EmployeeID="5" 
          OrderDate="1996-07-04T00:00:00">
      <OrderDetail ProductID="11" Quantity="12"/>
      <OrderDetail ProductID="42" Quantity="10"/>
   </Order>
</Customer>
<Customer CustomerID="LILAS" > 
     <ContactName>Carlos Gonzlez</ContactName>
   <Order OrderID="10283" CustomerID="LILAS" EmployeeID="3" 
          OrderDate="1996-08-16T00:00:00">
      <OrderDetail ProductID="72" Quantity="3"/>
   </Order>
</Customer>
</ROOT>'-- Create an internal representation of the XML document.
EXEC sp_xml_preparedocument @idoc OUTPUT, @doc-- Execute a SELECT statement using OPENXML rowset provider.
SELECT *
FROM OPENXML (@idoc, '/ROOT/Customer/Order',3)
         WITH (
       dali varchar(20) '../@CustomerID',
       OrderID  varchar(10),
               OrderDate datetime)EXEC sp_xml_removedocument @idoc

解决方案 »

  1.   

    DECLARE @idoc int
    DECLARE @doc varchar(1000)
    SET @doc ='
    <ROOT>
    <Customer>
       <CustomerID>VINET</CustomerID>
       <ContactName>Paul Henriot</ContactName>
       <Order OrderID="10248" CustomerID="VINET" EmployeeID="5" OrderDate="1996-07-04T00:00:00">
          <Peng>1</Peng>
          <Dali>2</Dali>
          <OrderDetail ProductID="11" Quantity="12"/>
          <OrderDetail ProductID="42" Quantity="10"/>
       </Order>
    </Customer>
    <Customer>   
       <CustomerID>LILAS</CustomerID>
       <ContactName>Carlos Gonzlez</ContactName>
       <Order OrderID="10283" CustomerID="LILAS" EmployeeID="3" OrderDate="1996-08-16T00:00:00">
          <Peng>11</Peng>
          <Dali>21</Dali>
          <OrderDetail ProductID="72" Quantity="3"/>
       </Order>
    </Customer>
    </ROOT>'
    -- Create an internal representation of the XML document.
    EXEC sp_xml_preparedocument @idoc OUTPUT, @doc
    -- Execute a SELECT statement using OPENXML rowset provider.
    SELECT    *
    FROM      OPENXML (@idoc, '/ROOT/Customer/Order',2)
               WITH (Peng  int,
                     Dali int)
    EXEC sp_xml_removedocument @idoc
      

  2.   

    CREATE TABLE T1(oid char(5), date datetime, amount float)
    DECLARE @idoc int
    DECLARE @doc varchar(1000)
    -- Sample XML document
    SET @doc ='
    <root>
      <Customer cid= "C1" name="Janine" city="Issaquah">
          <Order oid="O1" date="1/20/1996" amount="3.5" />
          <Order oid="O2" date="4/30/1997" amount="13.4">Customer was very 
                 satisfied</Order>
       </Customer>
       <Customer cid="C2" name="Ursula" city="Oelde" >
          <Order oid="O3" date="7/14/1999" amount="100" note="Wrap it blue 
                 white red">
              <Urgency>Important</Urgency>
          </Order>
          <Order oid="O4" date="1/20/1996" amount="10000"/>
       </Customer>
    </root>
    '
    --Create an internal representation of the XML document.
    EXEC sp_xml_preparedocument @idoc OUTPUT, @doc-- Execute a SELECT statement using OPENXML rowset provider.
    SELECT *
    FROM OPENXML (@idoc, '/root/Customer/Order', 1)
          WITH T1
    EXEC sp_xml_removedocument @idoc