将xml作为输入参数传递给存储过程
Posted 白羽轻飘
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将xml作为输入参数传递给存储过程相关的知识,希望对你有一定的参考价值。
1 查看存储过程的方式
exec sp_helptext \'GetTrackingFeedLogByMaxId\' select definition from sys.sql_modules where object_id=(OBJECT_ID(N\'GetTrackingFeedLogByMaxId\'))
2 在调用有输入参数的存储过程时必须包含参数,除非参数有默认值
3 系统存储过程sp_xml_preparedocument和OPENXML函数来操作传入的xml文档
DECLARE @XMLDocHandle int declare @xml varchar(1000)=\'<ROOT> <CurrencyList FromCurrencyCode="USD" ToCurrencyCode="AUD"></CurrencyList> <CurrencyList FromCurrencyCode="USD" ToCurrencyCode="EUR"></CurrencyList> <CurrencyList FromCurrencyCode="USD" ToCurrencyCode="GBP"></CurrencyList> <CurrencyList FromCurrencyCode="USD" ToCurrencyCode="MXN"></CurrencyList> </ROOT> \' execute sp_xml_preparedocument @XMLDocHandle output,@xml SELECT FromCurrencyCode,ToCurrencyCode FROM OPENXML(@XMLDocHandle,\'/ROOT/CurrencyList\',1) WITH ( FromCurrencyCode char(3), ToCurrencyCode char(3) )
注意变量必须定义为@XMLDocHandle变量
执行效果如下
3 xml作为存储过程的参数示例
--将xml作为存储过程的输入参数 create procedure getCurrentRatesXML @XMLList varchar(1000), @CurrencyRateDate datetime AS declare @XMLDocHandle int declare @CurrencyCodeTable table ( FromCurrencyCode char(3), ToCurrencyCode char(3) ) --准备xml文档 execute sp_xml_preparedocument @XMLDocHandle output,@XMLList; INSERT INTO @CurrencyCodeTable(FromCurrencyCode,ToCurrencyCode) SELECT FromCurrencyCode,ToCurrencyCode FROM OPENXML(@XMLDocHandle,\'/ROOT/CurrencyList\',1) WITH ( FromCurrencyCode char(3), ToCurrencyCode char(3) ); SELECT * FROM Sales.CurrencyRate cr JOIN @CurrencyCodeTable TVP ON cr.FromCurrencyCode=TVP.FromCurrencyCode AND cr.ToCurrencyCode=TVP.ToCurrencyCode WHERE CurrencyRateDate=@CurrencyRateDate; --移除文档 execute sp_xml_removedocument @XMLDocHandle; go
以上是关于将xml作为输入参数传递给存储过程的主要内容,如果未能解决你的问题,请参考以下文章
使用简单的 jdbc 调用将数组作为输入参数传递给 oracle 存储过程
如何通过不提交当前会话事务将 DataTable 作为参数传递给存储过程?