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' 
 
 
 
 
CompanyName="Company2">No Orders yet! 
 
'; 
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;
 

Sql Server存储过程传递XML参数_第1张图片

DECLARE @idoc int, @doc xml 
SET @doc =' 
 
 
     
        
        
   
 
 
 
     
        
   
 
 
'; 
--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

Sql Server存储过程传递XML参数_第2张图片

/ROOT/Customer/Order

Sql Server存储过程传递XML参数_第3张图片

/ROOT/Customer/Order/OrderDetail

Sql Server存储过程传递XML参数_第4张图片

 

你可能感兴趣的:(Sql Server存储过程传递XML参数)