declare @idoc int
declare @doc varchar(1000)
set @doc ='
<ROOT>
<Customer CustomerID="VINET" ContactName="Paul Henriot">
   <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">
   <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
-- SELECT stmt using OPENXML rowset provider
SELECT *
FROM   OPENXML (@idoc, '/ROOT/Customer/Order/OrderDetail',2)
         WITH (OrderID       int         '../@OrderID',
               CustomerID  varchar(10) '../@CustomerID',
               OrderDate   datetime    '../@OrderDate',
               ProdID      int         '@ProductID',
               Qty         int         '@Quantity')结果如下:OrderID CustomerID           OrderDate                 ProdID    Qty------------------------------------------------------------------------10248      VINET       1996-07-04 00:00:00.000   11      12
10248      VINET       1996-07-04 00:00:00.000   42      10
10283      LILAS       1996-08-16 00:00:00.000   72      3

解决方案 »

  1.   

    http://community.csdn.net/Expert/topic/4203/4203524.xml?temp=.6914026
      

  2.   

    http://blog.csdn.net/aw511/archive/2005/08/07/447656.aspx
      

  3.   

    谢谢大家的回答
    还有个疑惑,因为我xml文件是在数据库外面读入的,如果xml文件很大varchar变量似乎不能满足长度
    应该如何处理呢?
      

  4.   

    --处理的存储过程
    create proc pmod 
    @xml  text
    as
    DECLARE @idoc int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @xml

    SELECT * FROM OPENXML (@idoc, '/ModiTable/Row/Col',1)
    with(
    emp varchar(10) '../@emp',
    ele int '../@ele',
    [index] varchar(10) '@index',
    value decimal(20,2) '.'
    )

    EXEC sp_xml_removedocument @idoc
    go--调用存储过程
    exec pmod '
    <ModiTable>
    <Row emp = "chjl" ele ="4">
    <Col index="1">222.00</Col>
    <Col index="2">3.00</Col>
    <Col index="3">0.00</Col>
    <Col index="4">0.00</Col>
    <Col index="5">0.00</Col>
    </Row>
    <Row emp = "chzg" ele ="4">
    <Col index="1">2.66</Col>
    <Col index="2">234.00</Col>
    <Col index="3">5.00</Col>
    <Col index="4">6.00</Col>
    <Col index="5">4.00</Col>
    </Row>
    </ModiTable>
    '
    go--删除测试
    drop proc pmod/*--测试结果emp        ele         index      value                  
    ---------- ----------- ---------- ---------------------- 
    chjl       4           1          222.00
    chjl       4           2          3.00
    chjl       4           3          .00
    chjl       4           4          .00
    chjl       4           5          .00
    chzg       4           1          2.66
    chzg       4           2          234.00
    chzg       4           3          5.00
    chzg       4           4          6.00
    chzg       4           5          4.00(所影响的行数为 10 行)--*/
      

  5.   

    注意,是参数,因此应该在存储过程或者用户定义函数中使用. 不能直接定义text/ntext变量
      

  6.   

    zjcxc(邹建),非常感谢!
    俺另外开帖给分
    http://community.csdn.net/Expert/topic/4214/4214802.xml?temp=.8675959