本文为转载:原文地址

在SQL SERVER 2008 R2 上亲测可用,

这个存储过程配合SoapUI使用效果更好:参考地址

前提设置:http://www.cnblogs.com/chenxizhang/archive/2009/04/15/1436747.html

1.打开方面

2.方面选中"外围应用配置器"->OleAutomationEnabled->True

以下为存储过程

  1. CREATE proc [dbo].[spHTTPRequest]
  2. @URI varchar(2000) = '',
  3. @methodName varchar(50) = '',
  4. @requestBody varchar(8000) = '',
  5. @SoapAction varchar(255),
  6. @UserName nvarchar(100), -- Domain\UserName or UserName
  7. @Password nvarchar(100),
  8. @responseText varchar(8000) output
  9. as
  10. SET NOCOUNT ON
  11. IF @methodName = ''
  12. BEGIN
  13. select FailPoint = 'Method Name must be set'
  14. return
  15. END
  16. set @responseText = 'FAILED'
  17. DECLARE @objectID int
  18. DECLARE @hResult int
  19. DECLARE @source varchar(255), @desc varchar(255)
  20. EXEC @hResult = sp_OACreate 'MSXML2.ServerXMLHTTP', @objectID OUT
  21. IF @hResult <> 0
  22. BEGIN
  23. EXEC sp_OAGetErrorInfo @objectID, @source OUT, @desc OUT
  24. SELECT hResult = convert(varbinary(4), @hResult),
  25. source = @source,
  26. description = @desc,
  27. FailPoint = 'Create failed',
  28. MedthodName = @methodName
  29. goto destroy
  30. return
  31. END
  32. -- open the destination URI with Specified method
  33. EXEC @hResult = sp_OAMethod @objectID, 'open', null, @methodName, @URI, 'false', @UserName, @Password
  34. IF @hResult <> 0
  35. BEGIN
  36. EXEC sp_OAGetErrorInfo @objectID, @source OUT, @desc OUT
  37. SELECT hResult = convert(varbinary(4), @hResult),
  38. source = @source,
  39. description = @desc,
  40. FailPoint = 'Open failed',
  41. MedthodName = @methodName
  42. goto destroy
  43. return
  44. END
  45. -- set request headers
  46. EXEC @hResult = sp_OAMethod @objectID, 'setRequestHeader', null, 'Content-Type', 'text/xml;charset=UTF-8'
  47. IF @hResult <> 0
  48. BEGIN
  49. EXEC sp_OAGetErrorInfo @objectID, @source OUT, @desc OUT
  50. SELECT hResult = convert(varbinary(4), @hResult),
  51. source = @source,
  52. description = @desc,
  53. FailPoint = 'SetRequestHeader failed',
  54. MedthodName = @methodName
  55. goto destroy
  56. return
  57. END
  58. -- set soap action
  59. EXEC @hResult = sp_OAMethod @objectID, 'setRequestHeader', null, 'SOAPAction', @SoapAction
  60. IF @hResult <> 0
  61. BEGIN
  62. EXEC sp_OAGetErrorInfo @objectID, @source OUT, @desc OUT
  63. SELECT hResult = convert(varbinary(4), @hResult),
  64. source = @source,
  65. description = @desc,
  66. FailPoint = 'SetRequestHeader failed',
  67. MedthodName = @methodName
  68. goto destroy
  69. return
  70. END
  71. declare @len int
  72. set @len = len(@requestBody)
  73. EXEC @hResult = sp_OAMethod @objectID, 'setRequestHeader', null, 'Content-Length', @len
  74. IF @hResult <> 0
  75. BEGIN
  76. EXEC sp_OAGetErrorInfo @objectID, @source OUT, @desc OUT
  77. SELECT hResult = convert(varbinary(4), @hResult),
  78. source = @source,
  79. description = @desc,
  80. FailPoint = 'SetRequestHeader failed',
  81. MedthodName = @methodName
  82. goto destroy
  83. return
  84. END
  85. /*
  86. -- if you have headers in a table called RequestHeader you can go through them with this
  87. DECLARE @HeaderKey varchar(500), @HeaderValue varchar(500)
  88. DECLARE RequestHeader CURSOR
  89. LOCAL FAST_FORWARD
  90. FOR
  91. SELECT HeaderKey, HeaderValue
  92. FROM RequestHeaders
  93. WHERE Method = @methodName
  94. OPEN RequestHeader
  95. FETCH NEXT FROM RequestHeader
  96. INTO @HeaderKey, @HeaderValue
  97. WHILE @@FETCH_STATUS = 0
  98. BEGIN
  99. --select @HeaderKey, @HeaderValue, @methodName
  100. EXEC @hResult = sp_OAMethod @objectID, 'setRequestHeader', null, @HeaderKey, @HeaderValue
  101. IF @hResult <> 0
  102. BEGIN
  103. EXEC sp_OAGetErrorInfo @objectID, @source OUT, @desc OUT
  104. SELECT hResult = convert(varbinary(4), @hResult),
  105. source = @source,
  106. description = @desc,
  107. FailPoint = 'SetRequestHeader failed',
  108. MedthodName = @methodName
  109. goto destroy
  110. return
  111. END
  112. FETCH NEXT FROM RequestHeader
  113. INTO @HeaderKey, @HeaderValue
  114. END
  115. CLOSE RequestHeader
  116. DEALLOCATE RequestHeader
  117. */
  118. -- send the request
  119. EXEC @hResult = sp_OAMethod @objectID, 'send', null, @requestBody
  120. IF @hResult <> 0
  121. BEGIN
  122. EXEC sp_OAGetErrorInfo @objectID, @source OUT, @desc OUT
  123. SELECT hResult = convert(varbinary(4), @hResult),
  124. source = @source,
  125. description = @desc,
  126. FailPoint = 'Send failed',
  127. MedthodName = @methodName
  128. goto destroy
  129. return
  130. END
  131. declare @statusText varchar(1000), @status varchar(1000)
  132. -- Get status text
  133. exec sp_OAGetProperty @objectID, 'StatusText', @statusText out
  134. exec sp_OAGetProperty @objectID, 'Status', @status out
  135. select @status, @statusText, @methodName
  136. -- Get response text
  137. exec sp_OAGetProperty @objectID, 'responseText', @responseText out
  138. IF @hResult <> 0
  139. BEGIN
  140. EXEC sp_OAGetErrorInfo @objectID, @source OUT, @desc OUT
  141. SELECT hResult = convert(varbinary(4), @hResult),
  142. source = @source,
  143. description = @desc,
  144. FailPoint = 'ResponseText failed',
  145. MedthodName = @methodName
  146. goto destroy
  147. return
  148. END
  149. destroy:
  150. exec sp_OADestroy @objectID
  151. SET NOCOUNT OFF
  152.  
  153. 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

调用方法

  1. declare @xmlOut varchar(8000)
  2. Declare @RequestText as varchar(8000);
  3. set @RequestText=
  4. '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
  5. <soapenv:Header/>
  6. <soapenv:Body>
  7. <tem:CreateOrder>
  8. <!--Optional:-->
  9. <tem:OrderRequest>
  10. <tem:OrderId>200</tem:OrderId>
  11. <!--Optional:-->
  12. <tem:OrderName>something</tem:OrderName>
  13. </tem:OrderRequest>
  14. </tem:CreateOrder>
  15. </soapenv:Body>
  16. </soapenv:Envelope>'
  17. exec spHTTPRequest
  18. 'http://localhost/testwebservices/helloworldservice.asmx',
  19. 'POST',
  20. @RequestText,
  21. 'http://tempuri.org/CreateOrderForMe',
  22. '', '', @xmlOut out
  23. select @xmlOut

转载请注明出处,by lazyneal 2017

SQL Server 通过TSQL(存储过程)用MSXML去调用Webservice的更多相关文章

  1. Sql Server系列:存储过程

    1 存储过程简介 存储过程是使用T-SQL代码编写的代码段.在存储过程中,可以声明变量.执行条件判断语句等其他编程功能.在MS SQL Server 2012中存储过程主要分三类:系统存储过程.自定义 ...

  2. SQL SERVER 临时表导致存储过程重编译(recompile)的一些探讨

    SQLSERVER为了确保返回正确的值,或者处于性能上的顾虑,有意不重用缓存在内存里的执行计划,而重新编译执行计划的这种行为,被称为重编译(recompile).那么引发存储过程重编译的条件有哪一些呢 ...

  3. SQL Server 2012 T-SQL 新特性

    序列 Sequence SQL Server 现在将序列当成一个对象来实现,创建一个序列的例子语法如下: CREATE SEQUENCE DemoSequence START WITH 1 INCRE ...

  4. 理解性能的奥秘——应用程序中慢,SSMS中快(2)——SQL Server如何编译存储过程

    本文属于<理解性能的奥秘--应用程序中慢,SSMS中快>系列 接上文:理解性能的奥秘--应用程序中慢,SSMS中快(1)--简介 本文介绍SQL Server如何编译存储过程并使用计划缓存 ...

  5. SQL Server的嵌套存储过程中使用同名的临时表怪像浅析

      SQL Server的嵌套存储过程,外层存储过程和内层存储过程(被嵌套调用的存储过程)中可以存在相同名称的本地临时表吗?如果可以的话,那么有没有什么问题或限制呢? 在嵌套存储过程中,调用的是外层存 ...

  6. SQL Server基础之存储过程

      简单来说,存储过程就是一条或者多条sql语句的集合,可视为批处理文件,但是其作用不仅限于批处理.本篇主要介绍变量的使用,存储过程和存储函数的创建,调用,查看,修改以及删除操作. 一:存储过程概述 ...

  7. 【SQL Server】SQL Server基础之存储过程

    SQL Server基础之存储过程  阅读目录 一:存储过程概述 二:存储过程分类 三:创建存储过程 1.创建无参存储过程 2.修改存储过程 3.删除存储过程 4.重命名存储过程 5.创建带参数的存储 ...

  8. (转)SQL Server基础之存储过程(清晰使用)

    阅读目录 一:存储过程概述 二:存储过程分类 三:创建存储过程 1.创建无参存储过程 2.修改存储过程 3.删除存储过程 4.重命名存储过程 5.创建带参数的存储过程   简单来说,存储过程就是一条或 ...

  9. Sql Server数据库之存储过程

    阅读目录 一:存储过程概述 二:存储过程分类 三:创建存储过程 1.创建无参存储过程 2.修改存储过程 3.删除存储过程 4.重命名存储过程 5.创建带参数的存储过程   简单来说,存储过程就是一条或 ...

随机推荐

  1. KVM虚拟化技术(三)KVM环境预配

    一.平台操作系统安装 选择合适的操作系统,此处选用CentOS 7 系统可最小化安装,也可标准安装: 如果要远程连接,建议安装VNC-Server 将防火墙配置可通信,SELINUX设为permiss ...

  2. 一个漂亮的 PlaceHolder

    预览: 不知道为什么下面这个窗口中的 JavaScript 代码没有运行-_-||,想看实际效果就把下面的代码保存下来打开看吧. 代码: <!DOCTYPE HTML> <html ...

  3. swift杂记

    1.0   数据类型强转  范围小 --->范围大  不会丢失精度  : 范围大 ---> 范围小 ,可能丢失精度 如 :Int(4.2) = 4 :CGFloat(2) = 2.0 2. ...

  4. Nginx深度优化

    简介 1.隐藏版本号2.修改Nginx用户与组3.配置Nginx网页缓存时间4.实现Nginx的日志切割5.配置Nginx实现连接超时6.更改进程数7.配置Nginx实现网页压缩功能8.配置Nginx ...

  5. ceph部署手册

    CentOS7.2部署Luminous版Ceph-12.2.0 在CentOS7.2上安装部署Luminous版Ceph-12.2.0.由于ceph的Luminous版本默认使用bluestore作为 ...

  6. sudo su到root或到其它用户报这种错

    一般不会出现这种情况,除非你进入的这个目录已经被删了.然后切换用户就找不到这个目录,所以报错. 这种情况下我们只要去到一个正常的目录就好了,比如:cd ~ 然后再切换,ok.

  7. Genomic signatures of evolutionary transitions from solitary to group living(独居到社会性的转变)

    1.摘要 群居性的进化是进化的主要过渡之一,但其背后的基因组变化是未知的.我们比较了10种蜜蜂的基因组,它们的社会复杂性各不相同,代表了社会进化中的多种独立过渡,并报告了三项主要发现. 第一,许多重要 ...

  8. 1.5.1、CDH 搭建Hadoop在安装之前(定制安装解决方案---使用内部包裹存储库)

    使用内部包裹存储库 您可以通过托管内部存储库或手动将存储库文件复制到Cloudera Manager主机来为Cloudera Manager创建parcel存储库. 继续阅读: 托管内部包裹存储库 配 ...

  9. MyEclipse2014安装aptana插件

    1. 2. aptana插件下载地址 链接: https://pan.baidu.com/s/1sloiAK1 密码: a1nh 3. 4. 确认是否安装成功

  10. angularjs directive scope 与父scope双向绑定

    参考 http://www.jb51.net/article/83051.htm angluar.module("aaa").directive("testDirecti ...