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
时间: 2024-11-04 09:17:57