how to call web service from t-sql

僤鯓⒐⒋嵵緔 提交于 2021-01-28 18:17:50

问题


I'm using below code to call a web service, but its returning empty value.

Please help me on this:

Declare @Object as Int;
Declare @ResponseText as Varchar(8000);

Exec sp_OACreate 'MSXML2.XMLHTTP', @Object OUT;
Exec sp_OAMethod @Object, 'open', NULL, 'get', 'http://usadc-vsbbmd02:8085/rest/api/latest/plan/EP-AR','false'
Exec sp_OAMethod @Object, 'send'
Exec sp_OAMethod @Object, 'responseText', @ResponseText OUTPUT
Select @ResponseText
Exec sp_OADestroy @Object

回答1:


You should use SQLCLR or an external program for this. In SQL 20106 you can use R from TSQL, and in SQL 2017 you can use Python too. It's rarely a good idea to make web service calls from TSQL, and if you do you should generally be pulling from a queue. In which case you can use an external program.

The sp_oaxxx procs are old, hard to use, little known, require a dangerous server configuration, etc, etc.

That being said, here's some code I dug up from usenet I wrote a long, long time ago:

create procedure http_get( @sUrl varchar(200), @response varchar(8000) out)
As
begin
    Declare
      @obj   int
     ,@hr   int
     ,@status int
     ,@msg varchar(255)

       exec @hr = sp_OACreate 'MSXML2.ServerXMLHttp', @obj OUT
       if @hr < 0 begin Raiserror('sp_OACreate MSXML2.ServerXMLHttp failed', 16,1) return 1 end
       exec @hr = sp_OAMethod @obj, 'Open', NULL, 'GET', @sUrl, false
       if @hr <0 begin set @msg = 'sp_OAMethod Open failed' goto eh end
       exec @hr = sp_OAMethod @obj, 'send'
       if @hr <0 begin  set @msg = 'sp_OAMethod Send failed' goto eh end
       exec @hr = sp_OAGetProperty @obj, 'status', @status OUT
       if @hr <0 begin  set @msg = 'sp_OAMethod read status failed' goto eh end
       if @status <> 200  begin set @msg = 'sp_OAMethod http status ' + str(@status) goto eh end
       exec @hr = sp_OAGetProperty @obj, 'responseText', @response OUT
       if @hr <0 begin  set @msg = 'sp_OAMethod read response failed' goto eh end
       exec @hr = sp_OADestroy @obj
       return 0
    eh:
      exec @hr = sp_OADestroy @obj
      Raiserror(@msg, 16, 1)
      return 1
end



回答2:


I tend to call web services though my web servers. As David Browne correctly pointed out that it is really not recommended via TSQL.

However, (and just for fun) here is a working example on how to download and parse the lottery numbers.

Two side notes:

  1. Virtually no limit to the file size downloaded
  2. Clearly you would have to change the destination/source folders from c:\working\lottery.xml

Example

exec master..xp_cmdshell 'powershell.exe Invoke-WebRequest "http://data.ny.gov/resource/d6yy-54nr.xml" -OutFile "c:\working\lottery.xml"',no_output

Declare @XML xml; 
Select @XML = BulkColumn FROM  OPENROWSET(BULK 'c:\working\lottery.xml', SINGLE_BLOB) x; 

Select A.*
      ,Pos1  = B.Pos1
      ,Pos2  = B.Pos2
      ,Pos3  = B.Pos3
      ,Pos4  = B.Pos4
      ,Pos5  = B.Pos5
      ,PBall = B.Pos6
 From (
        Select [DrawDate]   = r.n.value('(draw_date)[1]','date')
              ,[Numbers]    = r.n.value('(winning_numbers)[1]','varchar(50)')
              ,[Multiplier] = r.n.value('(multiplier)[1]','varchar(50)')
         From  @XML.nodes('response/row/*') r(n)
      ) A      
 Cross Apply (
                Select Pos1 = ltrim(rtrim(xDim.value('/x[1]','varchar(max)')))
                      ,Pos2 = ltrim(rtrim(xDim.value('/x[2]','varchar(max)')))
                      ,Pos3 = ltrim(rtrim(xDim.value('/x[3]','varchar(max)')))
                      ,Pos4 = ltrim(rtrim(xDim.value('/x[4]','varchar(max)')))
                      ,Pos5 = ltrim(rtrim(xDim.value('/x[5]','varchar(max)')))
                      ,Pos6 = ltrim(rtrim(xDim.value('/x[6]','varchar(max)')))
                From  (Select Cast('<x>' + replace(A.Numbers,' ','</x><x>')+'</x>' as xml) as xDim) as A 
             ) B
 Order By DrawDate Desc

Returns



来源:https://stackoverflow.com/questions/46102766/how-to-call-web-service-from-t-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!