Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Web service:
C#
public class MyWebService : System.Web.Services.WebService
{
    public MyWebService()
    {
}

[WebMethod]
public string HelloWorld()
{
    return "Hello World";
}

Stored procedure:
SQL
CREATE PROC Hello

AS
    DECLARE @obj AS INT
    DECLARE @Uri AS NVARCHAR(MAX)
    DECLARE @Response AS BIT


    SET @Uri = 'http://MY IP IS HERE :9671/MyWebService.asmx/HelloWorld'
    EXEC sp_OACreate 'MSXML2.ServerXMLHttp.3.0', @obj OUT
    EXEC sp_OAMethod @obj, 'open', NULL, 'POST', @Uri, false
    EXEC sp_OAGetProperty @obj, 'ResponseText', @Response OUTPUT

    SELECT @Response [response]
    EXEC sp_OADestroy @obj

RETURN

Executing it like this:
SQL
EXEC Hello


The output after calling the stored procedure is always null but I want to set the web service return value as output. (My SQL SERVER 2012 CLR is Enable)
Posted
Updated 2-Jul-18 4:23am
Comments
Suvabrata Roy 3-Sep-14 1:04am    
I think sp_OACreate is unable to create the object

Try this code

@hr=sp_OACreate 'MSXML2.ServerXMLHttp.3.0', @obj OUT
IF @hr <> 0
BEGIN
EXEC sp_OAGetErrorInfo @object, @src OUT, @desc OUT
raiserror('Error Creating COM Component 0x%x, %s, %s',16,1, @hr, @src, @desc)
RETURN
END;
Roozbeh Amiressami 3-Sep-14 3:32am    
when I trace your code if condition never true and I got this xml code and I think @hr is always null.

can you explain me?

<!--?xml version="1.0" encoding="utf-8"?-->
<string xmlns="http://www.webserviceX.NET/"><StockQuotes><Stock><Symbol>MSFT</Symbol><Last>45.09</Last><Date>9/2/2014</Date><Time>4:15pm</Time><Change>-0.34</Change><Open>45.43</Open><High>45.46</High><Low>44.85</Low><Volume>22976776</Volume><MktCap>371.5B</MktCap><PreviousClose>45.43</PreviousClose><PercentageChange>-0.75%</PercentageChange><AnnRange>30.95 - 45.71</AnnRange><Earns>2.63</Earns><P-E>17.27</P-E><Name>Microsoft Corpora</Name></Stock></StockQuotes>
Suvabrata Roy 3-Sep-14 4:44am    
Is this the reply of your web service ?
Roozbeh Amiressami 3-Sep-14 5:04am    
yes
Suvabrata Roy 3-Sep-14 6:01am    
Your code and output not matched so please check you service URL and Operation Name

Now I see...
You never SEND your command to the service... (And it should be GET and not POST I think)...
Here is the SP...
SQL
CREATE PROC Hello
AS
    DECLARE @obj AS INT
    DECLARE @Uri AS NVARCHAR(MAX)
    DECLARE @Response AS BIT
 

    SET @Uri = 'http://MY IP IS HERE :9671/MyWebService.asmx/HelloWorld'
    EXEC sp_OACreate 'MSXML2.ServerXMLHttp.3.0', @obj OUT
    EXEC sp_OAMethod @obj, 'open', NULL, 'POST', @Uri, false
    EXEC sp_OAMethod @obj, 'send'
    EXEC sp_OAGetProperty @obj, 'ResponseText', @Response OUTPUT
 
    SELECT @Response [response]
    EXEC sp_OADestroy @obj
 
RETURN
 
Share this answer
 
I face the same problem today.

It's clear that without the post of Peter and adding the send method, there is not a big hope that it's works.

But it's not enough.

You need to replace this : DECLARE @Uri AS NVARCHAR(MAX)
with : DECLARE @Uri AS NVARCHAR(4000)

4000 is the underlying limitation of the object.
If you don't change MAX by 4000 you will still have a null value.

My context : W10 / SQL SERVER 2016
 
Share this answer
 
Comments
CHill60 9-Aug-17 14:33pm    
Really? Rubbish!
if you want to exec asmx on sql server please check your URL. Because "case sensitive"
url link on sql server. (Upper case -Lower Case)
 
Share this answer
 
Comments
Richard Deeming 19-Dec-17 11:29am    
Asked, answered, and solved over THREE YEARS AGO.

And your answer is nonsense. URLs on Windows are not case-sensitive.
I think you can do the following:
replace                         DECLARE @Response AS BIT  
by                              DECLARE @Response VARCHAR(8000)


ALTER PROC Hello
AS
    DECLARE @obj AS INT
    DECLARE @Uri AS NVARCHAR(MAX)
    DECLARE @Response VARCHAR(8000)
 

    SET @Uri = 'http://localhost:51017/WebService.asmx?op=HelloWorld'
    EXEC sp_OACreate 'MSXML2.ServerXMLHttp.3.0', @obj OUT
    EXEC sp_OAMethod @obj, 'open', NULL, 'POST', @Uri, false
    EXEC sp_OAMethod @obj, 'send'
    EXEC sp_OAGetProperty @obj, 'ResponseText', @Response OUTPUT
 
    SELECT @Response [response]
    EXEC sp_OADestroy @obj
 
RETURN

GO
 
Share this answer
 
Comments
CHill60 3-Jul-18 5:48am    
Essentially the same as Solution 1 posted over 4 years ago.
Stick to answering new posts where the OP still needs help and make sure you are bringing something new to the thread

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900