Simple way to invoke a webservice in stored procedure - sql

I an a bit amateur and I need to have my webservice called in sp so that I can give it an input right in sp and obtain the output but my sql code makes an error :sp_OACreate has not yet been called successfully for this command batch.
what is the solution? could anybody tell me simply?
I use sqlserver2012
and my sp is like:
create procedure [dbo].[test2]
#paramd int,
#paramm int,
#paramy int
as
begin
declare #obj int
declare #sUrl nvarchar(max)
declare #response varchar(8000)
declare #xml XML
set #sUrl= 'http://localhost:31876/myage/WebService.asmx?op=converttodaysweb?day:'+convert(nvarchar,#paramd)+'month:'+convert(nvarchar,#paramm)+'year:'+convert(nvarchar,#paramy)
exec sys.sp_OAMethod #obj,'Open',null,'GET',#sUrl,false
exec sys.sp_OAMethod #obj,send,null,''
exec sys.sp_OAGetProperty #obj,'responseXML.xml',#response OUT
SELECT #response [response]
exec sys.sp_OADestroy #obj
RETURN
END

Related

Return value from dynamic linked server stored procedure

I want to return a value from a dynamic linked server stored procedure.
--storedprocedure on the remote linked server
ALTER PROCEDURE [dbo].[SPTEST]
#DocNumOut VARCHAR(20) output
AS
BEGIN
SET NOCOUNT ON;
SELECT #DocNumOut=1;
RETURN #DocNumOut
END
On the local server, how can I retrieve the value of #DocNumOut? I have trial many combination using EXEC, openquery, EXEC sp_executesql, but none can return the value into my local variable, #DocNumberOUT.
--local server
DECLARE #DocNumberOUT as int;
DECLARE #SQLCMD AS VARCHAR(2000)='EXEC LinkedServer.MyDB.dbo.SPTEST'
EXEC #DocNumberOUT= sp_executesql #SQLCMD
--Procedure expects parameter '#statement' of type 'ntext/nchar/nvarchar'.
Use OUTPUT parameter on your Stored Procedure and don't use the return value.
Use the proper data type. Don't use varchar for everything. Since you assigned an integer to the variable, declare it as integer.
ALTER PROCEDURE [dbo].[SPTEST]
#DocNumOut INT output
AS
BEGIN
SET NOCOUNT ON;
SELECT #DocNumOut=1;
END
On the calling side, the error message is telling you that #SQLCMD should declare as NVARCHAR. Using sp_executesql is the right choice. But you should also define the parameters and pass in the variable also. #DocNumOut is the name of the parameter that you defined in SPTEST. #DocNumberOUT is your local variable
DECLARE #DocNumberOUT as int;
DECLARE #SQLCMD AS NVARCHAR(2000);
SET #SQLCMD = 'EXEC [LinkedServer].[MyDB].[dbo].[SPTEST] #DocNumOut = #DocNumberOUT OUTPUT'
EXEC sp_executesql #SQLCMD, N'#DocNumberOUT INT OUTPUT', #DocNumberOUT OUTPUT
SELECT #DocNumberOUT

SQL Server Formatmessage function unable to work with a variable within a procedure

Data setup to explain my problem
create table dbo.tbl_test1(
test1_id int,
val varchar(50)
);
create table dbo.tbl_test2(
test2_id int,
val varchar(50)
);
insert into dbo.tbl_test1 values(1, 'Hola');
insert into dbo.tbl_test2 values(1, 'Hi');
Following procedure when invoked throws error as shown in the screen shot
create procedure [dbo].[test_procedure]
#tbl_nm varchar(50),
#col_nm varchar(50),
#val varchar(max)
as
begin
declare #sqlPartial as nvarchar(max),
#sqlTmp as nvarchar(max),
#result as int
SET #sqlPartial = 'select #res=%s from %s where val=''%s'''
/* #sqlPartial as argument is source of error */
SELECT #sqlTmp=FORMATMESSAGE(#sqlPartial, #col_nm, #tbl_nm, #val)
print #sqlTmp
EXECUTE sp_executesql #sqlTmp, N'#res int OUTPUT', #res=#result OUTPUT
select #result
end
go
It seems FORMATMESSAGE is unable to work with variable #sqlPartial.
As soon as I replace #sqlPartial with the string from #sqlPartial, it works well like in following modified procedure
alter procedure [dbo].[test_procedure]
#tbl_nm varchar(50),
#col_nm varchar(50),
#val varchar(max)
as
begin
declare #sqlTmp as nvarchar(max),
#result as int
/* #sqlPartial replaced with content works */
SELECT #sqlTmp = FORMATMESSAGE('select #res=%s from %s where val=''%s''', #col_nm, #tbl_nm, #val)
print #sqlTmp
EXECUTE sp_executesql #sqlTmp, N'#res int OUTPUT', #res=#result OUTPUT
select #result
end
go
To execute this procedure use
exec dbo.test_procedure #tbl_nm='tbl_test1', #col_nm='test1_id', #val='Hola'
What am I not doing correctly?
If you're below 2017 (or is it 2016?), you can't use NVARCHAR(MAX) with FORMATMESSAGE, it's a system function actually used for reporting errors and so it has its limitations. You can use NVARCHAR(4000) (the max amount) though.

Calling Scalar Function on a linked Server

I am trying to call a scalar function on a linked server but I am having a little trouble setting it up. I am hoping to set it up as a function on my server.
Below is the best I came up with.
I am trying to wrap an openquery statement within the function on my server. However, the query works by itself by I am not able to return the results without causing an error.
USE POWERVIEW
GO
ALTER FUNCTION DBO.FN_VAR_DUMPNAME (#DUMPLOC NVARCHAR(40))
RETURNS NVARCHAR(40)
AS
BEGIN
--DECLARE #DUMPLOC NVARCHAR(40)='D11'
DECLARE #sql nvarchar(800)
DECLARE #param Nvarchar(20)= #DUMPLOC
DECLARE #retval NVARCHAR(40)
DECLARE #ParmDefinition nvarchar(500)=N'#retvalOUT NVARCHAR(40) OUTPUT'
DECLARE #innersql nvarchar(400)
SET #innersql = 'SELECT POWERVIEW.DBO.FN_VAR_DUMPNAME('''+''''+#param +''''+''')'
SET #sql = 'select * from openquery(MINESQLSERVER,'''+ #innersql +''' )'
***RETURN EXEC sp_executesql #sql --This line does not work***
END
This is too long for a comment.
SQL Server does not allow functions to call dynamic SQL. Hence you cannot do what you want.
You have other problems as well:
return exec is not something I've every seen before.
exec returns an integer.
The function returns a string.
You will need to solve your problem using some other method -- a stored procedure comes to mind.

How to store result of dynamically executed stored procedure into a variable?

I am trying to assign result of a stored procedure executed dynamically into a variable. However when i call them, sql throw me this error:
Could not find the stored procedure 'exec dbo.cont_com 3611,892;'
Below is the code snippet I am using:
declare #procedure_to_call varchar(max);
declare #procedure_result int;
set #procedure_to_call='exec dbo.cont_com 3611,892;'
exec #procedure_result=#procedure_to_call;
When I put parenthesis and execute the stored procedure, it works fine:
exec (#procedure_to_call);
But i need to save the return of the #procedure_to_call inside a variable.
This is the sample code for dbo.cont_com SP:
CREATE PROCEDURE dbo.cont_com
(
#id_c int,
#id_f int)
AS
BEGIN
declare #porcent int;
select #porcent=p.porcent
from porcent_table p
where p.id_c=#id_c
and p.id_c=#id_f;
select porcent=#porcent;
return #porcent;
END
Any help will be greatly appreciated.
Try something like this.
DECLARE #procedure_to_call NVARCHAR(MAX) = 'EXEC #x = dbo.cont_com 3611,892'
DECLARE #procedure_result INT;
SET #procedure_result = 0;
EXEC sp_executesql #procedure_to_call, N'#x INT OUT', #procedure_result OUT;
SELECT #procedure_result
It worked for me, hope it work for you.
set #procedure_to_call='exec dbo.cont_com 3611,892;'
exec #procedure_result=#procedure_to_call;
This becomes
exec exec dbo.cont_com 3611,892; -- hence the error
exec (#procedure_to_call) gets around this by exectuing what's in the parentheses as its own activity
Try:
set #procedure_to_call='dbo.cont_com 3611,892;'

Return Varchar value from Stored Procedure

I have the following basic stored Procedure which return varchar value
create proc myproc
AS
return 'SSS'
Now when I call this stored procedure using the following query
declare #ret varchAR(max)
exec sp_executesql N'exec #ret =procc',
N'#ret varchar(MAX) OUTPUT',#ret = #ret OUTPUT select #ret as result
I get error the following error
Conversion failed when converting the varchar value 'SSS' to data type int.
Kindly help.
Thanks
Stored procedures in MS SQL Sever can only return an Integer.
Did you want to use an output parameter instead?
CREATE PROC [myproc]
#output VARCHAR(3) OUTPUT
AS
SET #output = 'SSS';
RETURN 0;
Which you could call like this,
DECLARE #output VARCHAR(3);
EXEC [myproc] #output OUTPUT;
SELECT #output;
Or maybe you'd prefer to return a scalar result set?
CREATE PROC [myproc]
AS
SELECT 'SSS';
RETURN 0;
which would return the same by simply executing,
EXEC [myproc];
Since stored procedures in MS SQL Sever can only return an Integer, try selecting the value rather than returning it:
create proc myproc AS SELECT 'SSS'
Have output parameter
create proc myproc
(#value varchar(100) output)
AS
select #value='SSS'
GO
declare #ret varchAR(max)
exec sp_executesql N'exec #ret =procc',
N'#ret varchar(MAX) OUTPUT',#ret = #ret OUTPUT select #ret as result
If you want to return a value from stored procedure, you have to declare an output parameter for that.
Hope the below code will help you
create proc myproc
#outputvalue VARCHAR (16) output
AS
set #outputvalue='SSS'
select #outputvalue
alter procedure GetPrueba(#a int, #b int, #Suma INT OUTPUT, #Resta int OUTPUT) as
begin
select #Suma=#a+#b, #Resta=#a- #b
end
alter procedure getprueba2 as
begin
declare #s int, #r int
exec getprueba 2,5, #s OUTPUT, #r OUTPUT
select #s,#r
end
getprueba2