Calling scalar function through Exec - sql

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

Related

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

user defined function call vs calling UDF in a Store procedure

I have a user defined function that is called inside a stored procedure. All that stored procedure does is return the value that is obtained from the UDF scalar function.
However,
Select * UDF_FunctionName(param1,param2)
udf call is here-
SELECT dbo.udfFunction('1234',10) as result
and
Exec StoreProcName(param1,param2)
are returning different results.
StoreProcName calls the `UDF_FunctionName(param1,param2)
sproc code is here-
BEGIN
SET NOCOUNT ON;
Return SELECT [DbName].[dbo].[udfFunction](#param1, #param2)
END
What could be the reason for different results?.
You are trying to use RETURN and SELECT together:
Return SELECT [DbName].[dbo].[udfFunction](#param1, #param2)
You cannot do that, you either want to return the result, or select it. Depending on which you choose, retrieving the value will differ.
If you do this:
SELECT [DbName].[dbo].[udfFunction](#param1, #param2)
Then the resultset will have a single row and column containing your value. Access this exactly as you would any other resultset.
If you do this:
RETURN [DbName].[dbo].[udfFunction](#param1, #param2)
Your stored procedure will have a return value which is the result of your function call. Access this by defining a scalar variable and assigning it to the result of the SP call - assuming the result is INT that might look like
DECLARE #result INT
EXEC #Result = StoredProcName(#param1, #param2)
You should not use RETURN in this way in a stored procedure. If you want to return a scalar value to the calling code use an OUTPUT parameter. RETURN is generally used for status codes.
You might have something like
-- SAMPLE UDF
CREATE FUNCTION dbo.YourUDF (#Username VARCHAR(30), #EntityID INT)
RETURNS INT
AS
BEGIN
RETURN #EntityID;
END
GO
-- SAMPLE PROCEDURE
CREATE PROCEDURE dbo.YourProc #Username VARCHAR(30), #EntityID INT, #Output INT OUTPUT
AS
BEGIN
SET NOCOUNT ON;
SET #Output = dbo.YourUDF(#Username, #EntityID);
IF #Output IS NULL -- AN ERROR
BEGIN
RETURN 1; -- RETURN A STATUS OF -1 TO INDICATE ERROR
END
END
Then you could call this as:
DECLARE #Output INT, #ReturnValue INT;
EXECUTE #ReturnValue = dbo.YourProc
#Username = '1234',
#EntityID = 1,
#Output = #Output OUTPUT;
SELECT ValueFromUDF = #Output,
ReturnValue = #ReturnValue;
This returns:
ValueFromUDF ReturnValue
------------------------------
1 0
If we pass NULL as #EntityID, to trigger the artificial error, then we get a return status of -1:
DECLARE #Output INT, #ReturnValue INT;
EXECUTE #ReturnValue = dbo.YourProc
#Username = '1234',
#EntityID = NULL,
#Output = #Output OUTPUT;
SELECT ValueFromUDF = #Output,
ReturnValue = #ReturnValue;
ValueFromUDF ReturnValue
------------------------------
NULL 1

Get RETURN value from stored procedure in SQL

I have a stored procedure where it ends with a RETURN value of 0 or 1.
I want to use this value in an IF statement in another stored procedure.
How can I get the return value of the former stored procedure and save it in a variable in the latter?
I couldn't find anything related. All questions are about fetching the RETURN values in C#.
I was thinking, maybe something like this :
SP_Two
DECLARE #returnValue INT
SET #returnValue = EXEC SP_One
IF #returnValue = 1
BEGIN
--do something
END
ELSE
BEGIN
--do something else
END
This should work for you. Infact the one which you are thinking will also work:-
.......
DECLARE #returnvalue INT
EXEC #returnvalue = SP_One
.....
The accepted answer is invalid with the double EXEC (only need the first EXEC):
DECLARE #returnvalue int;
EXEC #returnvalue = SP_SomeProc
PRINT #returnvalue
And you still need to call PRINT (at least in Visual Studio).
Assign after the EXEC token:
DECLARE #returnValue INT
EXEC #returnValue = SP_One

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