SQL Server 通过TSQL(存储过程)用MSXML去调用Webservice
本文为转载:原文地址
在SQL SERVER 2008 R2 上亲测可用,
这个存储过程配合SoapUI使用效果更好:参考地址
前提设置:http://www.cnblogs.com/chenxizhang/archive/2009/04/15/1436747.html
1.打开方面
2.方面选中"外围应用配置器"->OleAutomationEnabled->True
以下为存储过程
- CREATE proc [dbo].[spHTTPRequest]
- @URI varchar(2000) = '',
- @methodName varchar(50) = '',
- @requestBody varchar(8000) = '',
- @SoapAction varchar(255),
- @UserName nvarchar(100), -- Domain\UserName or UserName
- @Password nvarchar(100),
- @responseText varchar(8000) output
- as
- SET NOCOUNT ON
- IF @methodName = ''
- BEGIN
- select FailPoint = 'Method Name must be set'
- return
- END
- set @responseText = 'FAILED'
- DECLARE @objectID int
- DECLARE @hResult int
- DECLARE @source varchar(255), @desc varchar(255)
- EXEC @hResult = sp_OACreate 'MSXML2.ServerXMLHTTP', @objectID OUT
- IF @hResult <> 0
- BEGIN
- EXEC sp_OAGetErrorInfo @objectID, @source OUT, @desc OUT
- SELECT hResult = convert(varbinary(4), @hResult),
- source = @source,
- description = @desc,
- FailPoint = 'Create failed',
- MedthodName = @methodName
- goto destroy
- return
- END
- -- open the destination URI with Specified method
- EXEC @hResult = sp_OAMethod @objectID, 'open', null, @methodName, @URI, 'false', @UserName, @Password
- IF @hResult <> 0
- BEGIN
- EXEC sp_OAGetErrorInfo @objectID, @source OUT, @desc OUT
- SELECT hResult = convert(varbinary(4), @hResult),
- source = @source,
- description = @desc,
- FailPoint = 'Open failed',
- MedthodName = @methodName
- goto destroy
- return
- END
- -- set request headers
- EXEC @hResult = sp_OAMethod @objectID, 'setRequestHeader', null, 'Content-Type', 'text/xml;charset=UTF-8'
- IF @hResult <> 0
- BEGIN
- EXEC sp_OAGetErrorInfo @objectID, @source OUT, @desc OUT
- SELECT hResult = convert(varbinary(4), @hResult),
- source = @source,
- description = @desc,
- FailPoint = 'SetRequestHeader failed',
- MedthodName = @methodName
- goto destroy
- return
- END
- -- set soap action
- EXEC @hResult = sp_OAMethod @objectID, 'setRequestHeader', null, 'SOAPAction', @SoapAction
- IF @hResult <> 0
- BEGIN
- EXEC sp_OAGetErrorInfo @objectID, @source OUT, @desc OUT
- SELECT hResult = convert(varbinary(4), @hResult),
- source = @source,
- description = @desc,
- FailPoint = 'SetRequestHeader failed',
- MedthodName = @methodName
- goto destroy
- return
- END
- declare @len int
- set @len = len(@requestBody)
- EXEC @hResult = sp_OAMethod @objectID, 'setRequestHeader', null, 'Content-Length', @len
- IF @hResult <> 0
- BEGIN
- EXEC sp_OAGetErrorInfo @objectID, @source OUT, @desc OUT
- SELECT hResult = convert(varbinary(4), @hResult),
- source = @source,
- description = @desc,
- FailPoint = 'SetRequestHeader failed',
- MedthodName = @methodName
- goto destroy
- return
- END
- /*
- -- if you have headers in a table called RequestHeader you can go through them with this
- DECLARE @HeaderKey varchar(500), @HeaderValue varchar(500)
- DECLARE RequestHeader CURSOR
- LOCAL FAST_FORWARD
- FOR
- SELECT HeaderKey, HeaderValue
- FROM RequestHeaders
- WHERE Method = @methodName
- OPEN RequestHeader
- FETCH NEXT FROM RequestHeader
- INTO @HeaderKey, @HeaderValue
- WHILE @@FETCH_STATUS = 0
- BEGIN
- --select @HeaderKey, @HeaderValue, @methodName
- EXEC @hResult = sp_OAMethod @objectID, 'setRequestHeader', null, @HeaderKey, @HeaderValue
- IF @hResult <> 0
- BEGIN
- EXEC sp_OAGetErrorInfo @objectID, @source OUT, @desc OUT
- SELECT hResult = convert(varbinary(4), @hResult),
- source = @source,
- description = @desc,
- FailPoint = 'SetRequestHeader failed',
- MedthodName = @methodName
- goto destroy
- return
- END
- FETCH NEXT FROM RequestHeader
- INTO @HeaderKey, @HeaderValue
- END
- CLOSE RequestHeader
- DEALLOCATE RequestHeader
- */
- -- send the request
- EXEC @hResult = sp_OAMethod @objectID, 'send', null, @requestBody
- IF @hResult <> 0
- BEGIN
- EXEC sp_OAGetErrorInfo @objectID, @source OUT, @desc OUT
- SELECT hResult = convert(varbinary(4), @hResult),
- source = @source,
- description = @desc,
- FailPoint = 'Send failed',
- MedthodName = @methodName
- goto destroy
- return
- END
- declare @statusText varchar(1000), @status varchar(1000)
- -- Get status text
- exec sp_OAGetProperty @objectID, 'StatusText', @statusText out
- exec sp_OAGetProperty @objectID, 'Status', @status out
- select @status, @statusText, @methodName
- -- Get response text
- exec sp_OAGetProperty @objectID, 'responseText', @responseText out
- IF @hResult <> 0
- BEGIN
- EXEC sp_OAGetErrorInfo @objectID, @source OUT, @desc OUT
- SELECT hResult = convert(varbinary(4), @hResult),
- source = @source,
- description = @desc,
- FailPoint = 'ResponseText failed',
- MedthodName = @methodName
- goto destroy
- return
- END
- destroy:
- exec sp_OADestroy @objectID
- SET NOCOUNT OFF
- GO
存储过程参数说明
1.@URI: the URI of the web service
2.@MethodName: this would be ‘GET’ or ‘POST’
3.@RequestBody: this is your SOAP xml that you want to send
4.@SoapAction: this the operation that you want to call on your service
5.@UserName: NT UserName if your web service requires authentication
6.@Password: the password if using NT Authentication on the web service
7.@ResponseText: this is an out parameter that contains the response from the web service
调用方法
- declare @xmlOut varchar(8000)
- Declare @RequestText as varchar(8000);
- set @RequestText=
- '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
- <soapenv:Header/>
- <soapenv:Body>
- <tem:CreateOrder>
- <!--Optional:-->
- <tem:OrderRequest>
- <tem:OrderId>200</tem:OrderId>
- <!--Optional:-->
- <tem:OrderName>something</tem:OrderName>
- </tem:OrderRequest>
- </tem:CreateOrder>
- </soapenv:Body>
- </soapenv:Envelope>'
- exec spHTTPRequest
- 'http://localhost/testwebservices/helloworldservice.asmx',
- 'POST',
- @RequestText,
- 'http://tempuri.org/CreateOrderForMe',
- '', '', @xmlOut out
- select @xmlOut
转载请注明出处,by lazyneal 2017
SQL Server 通过TSQL(存储过程)用MSXML去调用Webservice的更多相关文章
- Sql Server系列:存储过程
1 存储过程简介 存储过程是使用T-SQL代码编写的代码段.在存储过程中,可以声明变量.执行条件判断语句等其他编程功能.在MS SQL Server 2012中存储过程主要分三类:系统存储过程.自定义 ...
- SQL SERVER 临时表导致存储过程重编译(recompile)的一些探讨
SQLSERVER为了确保返回正确的值,或者处于性能上的顾虑,有意不重用缓存在内存里的执行计划,而重新编译执行计划的这种行为,被称为重编译(recompile).那么引发存储过程重编译的条件有哪一些呢 ...
- SQL Server 2012 T-SQL 新特性
序列 Sequence SQL Server 现在将序列当成一个对象来实现,创建一个序列的例子语法如下: CREATE SEQUENCE DemoSequence START WITH 1 INCRE ...
- 理解性能的奥秘——应用程序中慢,SSMS中快(2)——SQL Server如何编译存储过程
本文属于<理解性能的奥秘--应用程序中慢,SSMS中快>系列 接上文:理解性能的奥秘--应用程序中慢,SSMS中快(1)--简介 本文介绍SQL Server如何编译存储过程并使用计划缓存 ...
- SQL Server的嵌套存储过程中使用同名的临时表怪像浅析
SQL Server的嵌套存储过程,外层存储过程和内层存储过程(被嵌套调用的存储过程)中可以存在相同名称的本地临时表吗?如果可以的话,那么有没有什么问题或限制呢? 在嵌套存储过程中,调用的是外层存 ...
- SQL Server基础之存储过程
简单来说,存储过程就是一条或者多条sql语句的集合,可视为批处理文件,但是其作用不仅限于批处理.本篇主要介绍变量的使用,存储过程和存储函数的创建,调用,查看,修改以及删除操作. 一:存储过程概述 ...
- 【SQL Server】SQL Server基础之存储过程
SQL Server基础之存储过程 阅读目录 一:存储过程概述 二:存储过程分类 三:创建存储过程 1.创建无参存储过程 2.修改存储过程 3.删除存储过程 4.重命名存储过程 5.创建带参数的存储 ...
- (转)SQL Server基础之存储过程(清晰使用)
阅读目录 一:存储过程概述 二:存储过程分类 三:创建存储过程 1.创建无参存储过程 2.修改存储过程 3.删除存储过程 4.重命名存储过程 5.创建带参数的存储过程 简单来说,存储过程就是一条或 ...
- Sql Server数据库之存储过程
阅读目录 一:存储过程概述 二:存储过程分类 三:创建存储过程 1.创建无参存储过程 2.修改存储过程 3.删除存储过程 4.重命名存储过程 5.创建带参数的存储过程 简单来说,存储过程就是一条或 ...
随机推荐
- KVM虚拟化技术(三)KVM环境预配
一.平台操作系统安装 选择合适的操作系统,此处选用CentOS 7 系统可最小化安装,也可标准安装: 如果要远程连接,建议安装VNC-Server 将防火墙配置可通信,SELINUX设为permiss ...
- 一个漂亮的 PlaceHolder
预览: 不知道为什么下面这个窗口中的 JavaScript 代码没有运行-_-||,想看实际效果就把下面的代码保存下来打开看吧. 代码: <!DOCTYPE HTML> <html ...
- swift杂记
1.0 数据类型强转 范围小 --->范围大 不会丢失精度 : 范围大 ---> 范围小 ,可能丢失精度 如 :Int(4.2) = 4 :CGFloat(2) = 2.0 2. ...
- Nginx深度优化
简介 1.隐藏版本号2.修改Nginx用户与组3.配置Nginx网页缓存时间4.实现Nginx的日志切割5.配置Nginx实现连接超时6.更改进程数7.配置Nginx实现网页压缩功能8.配置Nginx ...
- ceph部署手册
CentOS7.2部署Luminous版Ceph-12.2.0 在CentOS7.2上安装部署Luminous版Ceph-12.2.0.由于ceph的Luminous版本默认使用bluestore作为 ...
- sudo su到root或到其它用户报这种错
一般不会出现这种情况,除非你进入的这个目录已经被删了.然后切换用户就找不到这个目录,所以报错. 这种情况下我们只要去到一个正常的目录就好了,比如:cd ~ 然后再切换,ok.
- Genomic signatures of evolutionary transitions from solitary to group living(独居到社会性的转变)
1.摘要 群居性的进化是进化的主要过渡之一,但其背后的基因组变化是未知的.我们比较了10种蜜蜂的基因组,它们的社会复杂性各不相同,代表了社会进化中的多种独立过渡,并报告了三项主要发现. 第一,许多重要 ...
- 1.5.1、CDH 搭建Hadoop在安装之前(定制安装解决方案---使用内部包裹存储库)
使用内部包裹存储库 您可以通过托管内部存储库或手动将存储库文件复制到Cloudera Manager主机来为Cloudera Manager创建parcel存储库. 继续阅读: 托管内部包裹存储库 配 ...
- MyEclipse2014安装aptana插件
1. 2. aptana插件下载地址 链接: https://pan.baidu.com/s/1sloiAK1 密码: a1nh 3. 4. 确认是否安装成功
- angularjs directive scope 与父scope双向绑定
参考 http://www.jb51.net/article/83051.htm angluar.module("aaa").directive("testDirecti ...