Can you call a webservice from TSQL code?

前端 未结 9 1626
野趣味
野趣味 2020-11-29 00:39

Is there a way to call out from a TSQL stored procedure or function to a webservice?

相关标签:
9条回答
  • 2020-11-29 00:47

    I been working for big/global companies around the world, using Oracle databases. We are consuming web services all time thru DB with store procedures and no issues, even those ones with heavy traffic. All of them for internal use, I mean with no access to internet, only inside the plant. I would recommend to use it but being really careful about how you design it

    0 讨论(0)
  • 2020-11-29 00:51

    You can do it with the embedded VB objects.

    First you create one VB object of type 'MSXML2.XMLHttp', and you use this one object for all of your queries (if you recreate it each time expect a heavy performance penalty).

    Then you feed that object, some parameters, into a stored procedure that invokes sp_OAMethod on the object.

    Sorry for the inprecise example, but a quick google search should reveal how the vb-script method is done.

    --

    But the CLR version is much....MUCH easier. The problem with invoking webservices is that they cannot keep pace with the DB engine. You'll get lots of errors where it just cannot keep up.

    And remember, web SERVICES require a new connection each time. Multiplicity comes into play. You don't want to open 5000 socket connections to service a function call on a table. Thats looney!

    In that case you'd have to create a custom aggregate function, and use THAT as an argument to pass to your webservice, which would return a result set...then you'd have to collate that. Its really an awkward way of getting data.

    0 讨论(0)
  • 2020-11-29 00:54

    Not in T-SQL code itself, but with SQL Server 2005 and above, they've enabled the ability to write CLR stored procedures, which are essentially functions in .NET code and then expose them as stored procedures for consumption. You have most of the .NET framework at your fingertips for this, so I can see consumption of a web service possible through this.

    It is a little lengthy to discuss in detail here, but here's a link to an MSDN article on the topic.

    0 讨论(0)
  • 2020-11-29 00:54

    In earlier versions of Sql, you could use either an extended stored proc or xp_cmdshell to shell out and call a webservice.

    Not that either of these sound like a decent architecture - but sometimes you have to do crazy stuff.

    0 讨论(0)
  • 2020-11-29 01:02

    Here'a an example to get some data from a webservice. In this case parse a user agent string to JSON.

    --first configure MSSQL to enable calling out to a webservice (1=true, 0=false)
    sp_configure 'show advanced options', 1;  
    GO  
    RECONFIGURE;  
    GO  
    sp_configure 'Ole Automation Procedures', 1;  
    GO  
    RECONFIGURE;  
    GO  
    
    CREATE PROCEDURE CallWebAPI_ParseUserAgent @UserAgent VARCHAR(512)
    AS
    BEGIN
        SET NOCOUNT ON;
    
        DECLARE @Object INT;
        DECLARE @ResponseText AS VARCHAR(8000);
        DECLARE @url VARCHAR(512)
    
        SET @url = 'http://www.useragentstring.com/?getJSON=all&uas=' + @UserAgent;
    
        EXEC sp_OACreate 'WinHttp.WinHttpRequest.5.1', @Object OUT;
        EXEC sp_OAMethod @Object, 'Open', NULL, 'GET', @url, 'false'
        EXEC sp_OAMethod @Object, 'setRequestHeader', NULL, 'Content-Type', 'application/json'
        EXEC sp_OAMethod @Object, 'send'
        EXEC sp_OAMethod @Object, 'responseText', @ResponseText OUTPUT
        SELECT @ResponseText
        EXEC sp_OADestroy @Object
    END
    
    --example how to call the API
    CallWebAPI_ParseUserAgent 'Mozilla/5.0 (Windows NT 6.2; rv:53.0) Gecko/20100101 Firefox/53.0'
    
    0 讨论(0)
  • 2020-11-29 01:03

    I would not do this for heavy traffic or mission critical stuff, HOWEVER, if you do NOT need to receive feedback from a service, then it is actually a great thing to do.

    Here is an example of what I have done.

    1. Triggers Insert and Update on a Table
    2. Trigger called Stored Proc that is passes the JSON data of the transaction to a Web Api Endpoint that then Inserts into a MongoDB in AWS.

    Don't do old XML

    JSON

    EXEC sp_OACreate 'WinHttp.WinHttpRequest.5.1', @Object OUT;
    EXEC sp_OAMethod @Object, 'Open', NULL, 'POST', 'http://server/api/method', 'false'
    EXEC sp_OAMethod @Object, 'setRequestHeader', null, 'Content-Type', 'application/json'
    DECLARE @len INT = len(@requestBody) 
    

    Full example:

    Alter Procedure yoursprocname
    
     @WavName varchar(50),
     @Dnis char(4) 
    
        AS
    BEGIN
    
        SET NOCOUNT ON;
    
    
    DECLARE @Object INT;
    DECLARE @Status INT;
    
    
    DECLARE @requestBody NVARCHAR(MAX) = '{
    "WavName": "{WavName}",
    "Dnis": "{Dnis}"
    }'
    
    
    SET @requestBody = REPLACE(@requestBody, '{WavName}', @WavName)
    SET @requestBody = REPLACE(@requestBody, '{Dnis}', @Dnis)
    
    
    EXEC sp_OACreate 'WinHttp.WinHttpRequest.5.1', @Object OUT;
    EXEC sp_OAMethod @Object, 'Open', NULL, 'POST',  'http://server/api/method', 'false'
    EXEC sp_OAMethod @Object, 'setRequestHeader', null, 'Content-Type', 'application/json'
    DECLARE @len INT = len(@requestBody) 
    EXEC sp_OAMethod @Object, 'setRequestHeader', null, 'Content-Length', @len
    EXEC sp_OAMethod @Object, 'send', null, @requestBody
    EXEC sp_OAGetProperty @Object, 'Status', @Status OUT
    EXEC sp_OADestroy @Object
    
    0 讨论(0)
提交回复
热议问题