Can you call a webservice from TSQL code?

前端 未结 9 1627
野趣味
野趣味 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 01:04

    Yes , you can create like this

    CREATE PROCEDURE CALLWEBSERVICE(@Para1 ,@Para2)
    AS
    BEGIN
        Declare @Object as Int;
        Declare @ResponseText as Varchar(8000);
    
        Exec sp_OACreate 'MSXML2.XMLHTTP', @Object OUT;
        Exec sp_OAMethod @Object, 'open', NULL, 'get', 'http://www.webservicex.com/stockquote.asmx/GetQuote?symbol=MSFT','false'
        Exec sp_OAMethod @Object, 'send'
        Exec sp_OAMethod @Object, 'responseText', @ResponseText OUTPUT
        Select @ResponseText
        Exec sp_OADestroy @Object
    END
    
    0 讨论(0)
  • 2020-11-29 01:05

    Sure you can, but this is a terrible idea.

    As web-service calls may take arbitrary amounts of time, and randomly fail, depending on how many games of counterstrike are being played on your network at the time, you can't tell how long this is going to take.

    At the bare minimum you're looking at probably half a second by the time it builds the XML, sends the HTTP request to the remote server, which then has to parse the XML and send a response back.

    1. Whichever application did the INSERT INTO BLAH query which caused the web-service to fire is going to have to wait for it to finish. Unless this is something that only happens in the background like a daily scheduled task, your app's performance is going to bomb

    2. The web service-invoking code runs inside SQL server, and uses up it's resources. As it's going to take a long time to wait for the HTTP request, you'll end up using up a lot of resources, which will again hurt the performance of your server.

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

    If you're working with sql 2000 compatibility levels and cannot do clr integration, see http://www.vishalseth.com/post/2009/12/22/Call-a-webservice-from-TSQL-(Stored-Procedure)-using-MSXML.aspx

    0 讨论(0)
提交回复
热议问题