SSMS:从REST调用中导入数据

时间:2018-09-26 10:09:34

标签: json sql-server rest tsql ssms

我想看看是否可以从对URL的REST调用中将数据导入MSSQL。我希望将来将此代码重用于假设的存储过程。这只是想成为概念证明。

此URL包含一些可用于实验的JSON:

http://api.plos.org/search?q=title:DNA

我检查URL是否用Postman返回一些JSON:

enter image description here 一切都很好。

我现在遵循this guide,它告诉我如何构造调用,这是我的代码:

DECLARE @Object AS INT;
DECLARE @ResponseText AS nvarchar(max);

EXEC sp_OACreate 'MSXML2.XMLHTTP', @Object OUT;
EXEC sp_OAMethod @Object, 'open', NULL, 'GET','http://api.plos.org/search?q=title:DNA', 'false'
EXEC sp_OAMethod @Object, 'send'
EXEC sp_OAMethod @Object, 'responseText', @ResponseText OUTPUT
SELECT @ResponseText
EXEC sp_OADestroy @Object

但不幸的是,它返回了一个NULL列。

enter image description here

我在做什么错?

有没有人通过SSMS发出REST呼叫?

2 个答案:

答案 0 :(得分:1)

我建议您使用SSIS(SQL Server集成服务)代替SSMS。 您可以查看以下链接:

Reference Link 1

Reference Link 2

答案 1 :(得分:0)

I found what was the problem, on line 3:

  1. NVARCHAR(MAX) can handle only 4000 characters
  2. VARCHAR(MAX) can handle only 4000 characters
  3. VARCHAR(8000) can handle 8000 characters

So if I try to query an URL that return less data everything is fine:

DECLARE @obj AS INT
DECLARE @Uri AS NVARCHAR(4000)
DECLARE @Response AS VARCHAR(8000)

SET @Uri = 'http://overpass-api.de/api/interpreter?data=area[name="Nelson"]->.a;(node(area.a)[amenity=cinema];way(area.a)[amenity=cinema];rel(area.a)[amenity=cinema];);out;'
EXEC sp_OACreate 'MSXML2.ServerXMLHttp.3.0', @obj OUT
EXEC sp_OAMethod @obj, 'open', NULL, 'GET', @Uri, false
EXEC sp_OAMethod @obj, 'send'
EXEC sp_OAGetProperty @obj, 'ResponseText', @Response OUTPUT

SELECT @Response [response] 
EXEC sp_OADestroy @obj

enter image description here

But the problem still persists above 8000 characters.

There was space for another question, if you want to dig further follow my next question here