Return Varchar value from Stored Procedure - sql

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

Related

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.

Simple way to invoke a webservice in stored procedure

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

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;'

Calling scalar function through Exec

I have created a scalar function
CREATE FUNCTION dbo.Dumm()
returns INT
AS
BEGIN
DECLARE #a INT
SELECT #a = 1
RETURN #a
END
Now I am calling the scalar function through Exec not through select
EXEC dbo.Dumm
It did not return 1. It just says
Command(s) completed successfully.
Whats happening internally. Is there any meaning for it ?
Try this:
DECLARE #ret int;
EXEC #ret = dbo.Dumm
and then show the result querying your variable #ret as follow:
SELECT #ret
Tell me if it's OK

Get scalar value from SELECT statement in stored proc, from within a stored proc

I know the preferred method for returning scalar values from stored procs is either using RETURN or an OUTPUT parameter. But lets say that I have a stored proc that returns the value using a select statement:
CREATE PROC spReturnNumber AS
SELECT 1
Is it possible to get this value from within another stored proc?
CREATE PROC spCheckNumber AS
EXEC spReturnNumber -- <-- get the return value here?
Clarification: I need a solution that doesn't require using an OUTPUT parameter, or using RETURN to return the value.
Thanks in advance.
You could use insert-exec to store the result of a stored procedure in a table:
declare #t table (col1 int)
insert #t exec spReturnNumber
return (select col1 from #t)
The definition of the table has to match the result set of the stored procedure.
Use an OUTPUT parameter instead of (or in addition to, if this procedure is used by other applications) the SELECT.
ALTER PROCEDURE dbo.spReturnNumber
#Number INT OUTPUT
AS
BEGIN
SET NOCOUNT ON;
SET #Number = 1;
SELECT #Number;
END
GO
CREATE PROCEDURE dbo.spCheckNumber
AS
BEGIN
SET NOCOUNT ON;
DECLARE #Number INT;
EXEC dbo.spReturnNumber #Number = #Number;
SELECT #Number;
END
GO
If you can't change the original procedure, but you know its output will remain static, you could use a #temp table.
CREATE PROCEDURE dbo.spCheckNumber
AS
BEGIN
SET NOCOUNT ON;
CREATE TABLE #n(i INT);
INSERT #n(i) EXEC dbo.spReturnNumber;
DECLARE #Number INT;
SELECT #Number = i FROM #n;
END
GO
You can't get the SELECT value from "parent" procedure but you can get the return value like this:
CREATE PROC A AS
BEGIN
DECLARE #ret int
EXEC #ret = spReturnNumber
RETURN #ret
END
If you are unable to change the proc being called .. place the result set in a temp table [or table variable]:
CREATE TABLE #results (val INT)
DECLARE #someval int
INSERT #results
EXEC dbo.spCheckNumber
SELECT #someval =val from #results