zoukankan      html  css  js  c++  java
  • Sql Server存储过程传递XML参数

    Create tables for later population using OPENXML. 
    CREATE TABLE Customers (CustomerID varchar(20) primary key, 
                     ContactName varchar(20),  
                     CompanyName varchar(20)); 
    GO 
    CREATE TABLE Orders( CustomerID varchar(20), OrderDate datetime);
    GO 
    DECLARE @docHandle int; 
    DECLARE @xmlDocument nvarchar(max); -- or xml type 
    SET @xmlDocument = N'<ROOT> 
    <Customers CustomerID="XYZAA" ContactName="Joe" CompanyName="Company1"> 
    <Orders CustomerID="XYZAA" OrderDate="2000-08-25T00:00:00"/> 
    <Orders CustomerID="XYZAA" OrderDate="2000-10-03T00:00:00"/> 
    </Customers> 
    <Customers CustomerID="XYZBB" ContactName="Steve" 
    CompanyName="Company2">No Orders yet! 
    </Customers> 
    </ROOT>'; 
    EXEC sp_xml_preparedocument @docHandle OUTPUT, @xmlDocument; 
    -- Use OPENXML to provide rowset consisting of customer data. 
    INSERT Customers  
    SELECT *  
    FROM OPENXML(@docHandle, N'/ROOT/Customers')  
       WITH Customers; 
    -- Use OPENXML to provide rowset consisting of order data. 
    INSERT Orders  
    SELECT *  
    FROM OPENXML(@docHandle, N'//Orders')  
       WITH Orders; 
    -- Using OPENXML in a SELECT statement. 
    SELECT * FROM OPENXML(@docHandle, N'/ROOT/Customers/Orders')
       WITH (CustomerID nchar(5) '../@CustomerID', OrderDate datetime);
    -- Remove the internal representation of the XML document. 
    EXEC sp_xml_removedocument @docHandle;
     

    image

    DECLARE @idoc int, @doc xml 
    SET @doc =' 
    <ROOT> 
    <Customer CustomerID="VINET" ContactName="Paul Henriot"> 
        <Order CustomerID="VINET" EmployeeID="5" OrderDate="1996-07-04T00:00:00"> 
           <OrderDetail OrderID="10248" ProductID="11" Quantity="12"/> 
           <OrderDetail OrderID="10248" ProductID="42" Quantity="10"/> 
        </Order> 
    </Customer> 
    <Customer CustomerID="LILAS" ContactName="Carlos Gonzlez"> 
        <Order CustomerID="LILAS" EmployeeID="3" OrderDate="1996-08-16T00:00:00"> 
           <OrderDetail OrderID="10283" 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 that uses the OPENXML rowset provider. 
    SELECT    * 
    FROM       OPENXML (@idoc, '/ROOT/Customer',1) 
                 WITH (CustomerID  varchar(10), 
                       ContactName varchar(20)); 
    EXEC sp_xml_removedocument @idoc

    image

    /ROOT/Customer/Order

    image

    /ROOT/Customer/Order/OrderDetail

    image

     

  • 相关阅读:
    刷链表的题都不要用Python
    [踩坑] @RequestBody注解转换modal报400错误问题排查与解决
    netty学习笔记二——ByteBuf类原理
    netty学习笔记一:TCP粘包拆包
    OkHttp3出现java.io.IOException: Hostname was not verified解决方案
    nginx学习笔记(一) 用nginx实现本地https请求转http请求
    zookeeper启动失败排查
    spring boot升级到2.0.0.M7后报错ConverterNotFoundException for java.time.Duration的解决方案
    JpaRepository QueryByExample方法使用详解
    JavaScript面向对象编程
  • 原文地址:https://www.cnblogs.com/lihuali/p/12941536.html
Copyright © 2011-2022 走看看