存储过程间XML参数传递的实例

--测试数据准备
Create table Product (ProductID varchar(18),cName varchar(50))
insert into Product Select '80100212800','A'
insert into Product Select '80100250006','B'
insert into Product Select '80101038000','C'
-- 测试执行    
 Exec ProductGet
 
 
--- 第一层代码
Alter PROCEDURE ProductGet
AS
Begin
 Declare @sql as nvarchar(max)
 Declare @cXML as XML
 Declare @cXML1 as XML
 Declare @cXML2 as XML
 Declare @cXML3 as XML
 
    Exec ProductOne '80100212800',@cXML1 Out
    Exec ProductOne '80100250006',@cXML2 Out
    Exec ProductOne '80101038000',@cXML3 Out
    --Set @cXML1 = @cXML1 + @cXML2 + @cXML3

 
   Declare @cXML_ as XML
  Set @cXML_ = (Select DISTINCT  * From (
             SELECT
            x.item.value('@OrderID[1]', 'VARCHAR(20)') AS OrderID,
            x.item.value('@ProductID[1]', 'VARCHAR(20)') AS ProductID
        FROM @cXML3.nodes('//items/item') AS x(item)    
    Union all 
     SELECT
            x.item.value('@OrderID[1]', 'VARCHAR(20)') AS OrderID,
            x.item.value('@ProductID[1]', 'VARCHAR(20)') AS ProductID
        FROM @cXML2.nodes('//items/item') AS x(item)    
            Union ALl
     SELECT
            x.item.value('@OrderID[1]', 'VARCHAR(20)') AS OrderID,
            x.item.value('@ProductID[1]', 'VARCHAR(20)') AS ProductID
        FROM @cXML1.nodes('//items/item') AS x(item)    
    ) A  FOR XML RAW('item'), ROOT('items'), TYPE)
    
  SET @cXML = @cXML_
 
    Select @cXML1
   SELECT
            x.item.value('@OrderID[1]', 'VARCHAR(20)') AS OrderID,
            x.item.value('@ProductID[1]', 'VARCHAR(20)') AS ProductID
        FROM @cXML1.nodes('//items/item') AS x(item)    
 
End


--- 第二层
Alter PROCEDURE ProductOne
    @cProductID as varchar(18)
    ,@cXML as XML Out
AS
Begin
 Declare @sql as nvarchar(max)
 --Declare @cXML as XML
 SET @cXML = (SELECT Top 10 'T1' as OrderID,ProductID  FROM Product 
                        Where ProductID >= @cProductID 
                        FOR XML RAW('item'), ROOT('items'), TYPE)
 
     set @cXML.modify('insert<item  OrderID = "T3" ProductID="11TEST" />
        after (/items/item)[1]       
')

      Exec ProductTwO @cProductID,@cXML Out     
    

End

--第三层
Alter PROCEDURE ProductTwO
    @cProductID as varchar(18)
    ,@cXML as XML Out   
AS
Begin
  Declare @cXML_ as XML
  Set @cXML_ = (Select * From (
        Select   'T2' as OrderID,ProductID From  Product Where ProductID = @cProductID
            Union ALl
     SELECT
            x.item.value('@OrderID[1]', 'VARCHAR(20)') AS OrderID,
            x.item.value('@ProductID[1]', 'VARCHAR(20)') AS ProductID
        FROM @cXML.nodes('//items/item') AS x(item)    
    ) A  FOR XML RAW('item'), ROOT('items'), TYPE)
  SET @cXML = @cXML_
End

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值