Ole Automation Procedure返回null

时间:2019-03-27 14:13:47

标签: sql-server tsql ole-automation

我有以下问题。在我们的服务器上运行以下sql会返回预期结果。在另一台服务器上运行该命令不会返回任何值。

执行以下操作:

sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Ole Automation Procedures', 1;
GO
RECONFIGURE;
GO

USE tempdb
GO

IF OBJECT_ID('tempdb..#xml') IS NOT NULL DROP TABLE #xml
CREATE TABLE #xml ( yourXML XML )
GO

DECLARE @URL VARCHAR(8000)

--DECLARE @QS varchar(50)

-- & or ? depending if there are other query strings
-- Use this for when there is other query strings:
--SELECT @QS = '&date='+convert(varchar(25),getdate(),126)
-- Use this for when there is NO other query strings:
-- SELECT @QS = '?date='+convert(varchar(25),getdate(),126)
SELECT @URL = 'http://exampleURL' -- + @QS

DECLARE @Response varchar(8000)
DECLARE @XML xml
DECLARE @Obj int
DECLARE @Result int
DECLARE @HTTPStatus int
DECLARE @ErrorMsg varchar(MAX)

EXEC @Result = sp_OACreate 'MSXML2.XMLHttp', @Obj OUT

EXEC @Result = sp_OAMethod @Obj, 'open', NULL, 'GET', @URL, false
EXEC @Result = sp_OAMethod @Obj, 'setRequestHeader', NULL, 'Content-Type', 'application/x-www-form-urlencoded'
EXEC @Result = sp_OAMethod @Obj, send, NULL, ''
EXEC @Result = sp_OAGetProperty @Obj, 'status', @HTTPStatus OUT

INSERT #xml ( yourXML )
EXEC @Result = sp_OAGetProperty @Obj, 'responseXML.xml'--, @Response OUT


declare @input XML=(
SELECT
yourXML
from
#xml)

SELECT
Item.value('(Code)[1]', 'nvarchar(max)') as Code,
Item.value('(Description)[1]', 'varchar(max)') as Description,
Item.value('(ImageUrl)[1]', 'nvarchar(max)') as ImageUrl
from
@input.nodes('//product') AS T(Item)

在第二台服务器中,@input返回null。有一个代理可以访问服务器上的站点,并且它与sql server 2008一起运行。

有什么想法为什么要使用空值?

2 个答案:

答案 0 :(得分:0)

我只是遇到了类似的问题,使用Ole Automation的查询突然返回null,没有任何错误。将'MSXML2.XMLHttp'更改为'MSXML2.ServerXMLHTTP'后,它再次开始工作。

要详细了解这两者之间的区别,请参阅article和Microsoft documentation。我从Microsoft网站复制了一些内容,以防将来两个网站都消失。

  

ServerXMLHTTP对象提供的功能类似于XMLHTTP对象。但是,与XMLHTTP不同,ServerXMLHTTP对象不依赖WinInet控件对远程XML文档进行HTTP访问。 ServerXMLHTTP使用新的HTTP客户端堆栈。 WinInet的此服务器安全子集是为服务器应用程序设计的,具有以下优点:

     
      
  • 可靠性— HTTP客户端堆栈可提供更长的正常运行时间。新的HTTP子集不包括WinInet功能,这些功能对于服务器应用程序不是很关键,例如URL缓存,代理服务器的自动发现,HTTP / 1.1分块,脱机支持以及对Gopher和FTP协议的支持。
  •   
  • 安全性-HTTP客户端堆栈不允许与其他用户的会话共享特定于用户的状态。 ServerXMLHTTP支持客户端证书。
  •   

答案 1 :(得分:0)

因此,看来我的问题是我无法从SQL服务器通过Internet进行访问,因此我必须使用以下行来设置代理。

exec @Result = sp_OAMethod @Obj, 'setProxy', NULL, '2', 'http://myProxy'

排序完成后,我就设法得到了结果。