我想看看是否可以从对URL的REST调用中将数据导入MSSQL。我希望将来将此代码重用于假设的存储过程。这只是想成为概念证明。
此URL包含一些可用于实验的JSON:
http://api.plos.org/search?q=title:DNA
我检查URL是否用Postman返回一些JSON:
我现在遵循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
列。
我在做什么错?
有没有人通过SSMS发出REST呼叫?
答案 0 :(得分:1)
答案 1 :(得分:0)
I found what was the problem, on line 3:
NVARCHAR(MAX)
can handle only 4000 charactersVARCHAR(MAX)
can handle only 4000 charactersVARCHAR(8000)
can handle 8000 charactersSo 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
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