Assign a variable to the output of a stored procedure - sql

I am trying to assign the output of a stored procedure to a variable using T-SQL
I have a stored procedure that I pass a varbinary(128) variable and it decrypts it using a key and has an output variable for the decrypted value. I'm not sure if my syntax is correct because when I run the procedure it gives the correct result but when I assign to a variable, then select the variable it is always null
declare #e as varbinary(128)
set #e = *encrypted value*
declare #t as int
set #t = 0
exec *storedprocedure* #data = #e,#t output
select #t
t is null when I select above

Here is a simple example
CREATE PROC myProc #in INT, #out INT OUTPUT
AS
SELECT #out = #in+20
GO
DECLARE #in INT, #out INT
SET #in = 1
EXEC myProc #in= #in, #out=#out OUTPUT
SELECT #out
DROP PROC myProc

Related

Stored procedured doesnt give the result back

I have written such a stored procedure, it must return a result, but it doesnt do that. it returns only a message that stored procedure runs successfully.
How should I change my SP?
CREATE PROCEDURE TestTVP
(
#param1 int,
#param2 int,
#a int OUTPUT
)
as
SET NOCOUNT ON
DECLARE #T1 as TABLE
(
PK INT IDENTITY NOT NULL,
Wert INTEGER,
Name INTEGER
)
INSERT INTO #T1(Wert,Name) VALUES (#param1,#param2)
return select count(*) from #T1
GO
exec TestTVP '1','22'
you have to pass OUTPUT parameter
declare #z int
exec TestTVP '1','22' ,#z output
and remove return from Stored Procedure make it only
...
select count(*) from #T1
You can use out parameter if you want the output :
DECLARE #Z int
EXEC TestTVP '2', '22',#z out

Set variables from stored procedure from results of another stored procedure

How can I do something along the following lines?
DECLARE #Var1 VARCHAR(200)
DECLARE #Var2 VARCHAR(200)
SET #Var1 = value1 from sp_someotherprocedure(#id)
SET #Var2 = value1 from sp_someotherprocedure(#id)
I've created a simple example how to from results of stored procedure.
In order to set a variable to the return value from stored procedure you need to use
OUTPUT parameter with stored procedure, which will return the value.
CREATE PROCEDURE YourStoredProcedure
(
#i int,
#result int OUTPUT
)
AS
BEGIN
SET #i = 1
Set #result = #i + 10
return #result
END
-- Code to set the output paramter
DECLARE #outputparameter AS int
exec #outputparameter = YourStoredProcedure 10, #outputparameter output
print #outputparameter
Like this:
EXEC #Var1 = sp_someotherprocedure(#id)
Stored procedures could return many result sets. Therefore, it is not meant to be used with a SELECT.
They Could be used in a case where you have OUTPUT parameters
You should look into Functions and then SELECT from it.
CREATE FUNCTION SalesByStore (#storeid varchar(30))
RETURNS TABLE AS
RETURN (SELECT t.title, s.qty
FROM sales s
JOIN titles t ON t.title_id = s.title_id
WHERE s.stor_id = #storeid)
Then you call it like :
SELECT * FROM SalesByStore('6380')
I'm pretty late to the party on this one, but just ran into this issue myself. The solution my team and I came up with is to execute the result of the sproc into a temp table, then select top 1 (or whatever meets your criteria) into local variable(s). So using the OP's example might look something like this:
DECLARE #Var1 VARCHAR(200)
DECLARE #Var2 VARCHAR(200)
DECLARE #sprocResult TABLE(val1 INT, val2 INT)
INSERT INTO #sprocResult (val1, val2)
EXEC sp_someotherprocedure #id
SELECT TOP 1
#Var1 = val1,
#Var2 = val2
FROM #sprocResult

how to SET store procedure output value to a declare variable

My Store procedure will return a ID after insert Date , i am successful insert to data , but every time my #test value will be 0 , but actually my Store procedure is returning a ID of 123 for example .
My code as below
declare #INS_ID int, #test int
EXEC #test = Ins_Data 4,'Apple','Apple','Apple',#INS_ID OUTPUT
select #test
It seems like you are confusing stored procedure return values with output parameters - consider this procedure
CREATE PROCEDURE dbo.TestProc #i INT OUTPUT
AS
SET #i = 2;
RETURN 1;
If you then call
DECLARE #a INT, #b INT;
EXECUTE #a = dbo.TestProc #b OUT;
SELECT #a, #b;
#a will be 1 (because of RETURN 1 in the procedure), #b will be 2 because it is set as 2 within the procedure.
Demo on SQL Fidlle

How to return the output of stored procedure into a variable in sql server

I want to execute a stored procedure in SQL Server and assign the output to a variable (it returns a single value) ?
That depends on the nature of the information you want to return.
If it is a single integer value, you can use the return statement
create proc myproc
as
begin
return 1
end
go
declare #i int
exec #i = myproc
If you have a non integer value, or a number of scalar values, you can use output parameters
create proc myproc
#a int output,
#b varchar(50) output
as
begin
select #a = 1, #b='hello'
end
go
declare #i int, #j varchar(50)
exec myproc #i output, #j output
If you want to return a dataset, you can use insert exec
create proc myproc
as
begin
select name from sysobjects
end
go
declare #t table (name varchar(100))
insert #t (name)
exec myproc
You can even return a cursor but that's just horrid so I shan't give an example :)
You can use the return statement inside a stored procedure to return an integer status code (and only of integer type). By convention a return value of zero is used for success.
If no return is explicitly set, then the stored procedure returns zero.
CREATE PROCEDURE GetImmediateManager
#employeeID INT,
#managerID INT OUTPUT
AS
BEGIN
SELECT #managerID = ManagerID
FROM HumanResources.Employee
WHERE EmployeeID = #employeeID
if ##rowcount = 0 -- manager not found?
return 1;
END
And you call it this way:
DECLARE #return_status int;
DECLARE #managerID int;
EXEC #return_status = GetImmediateManager 2, #managerID output;
if #return_status = 1
print N'Immediate manager not found!';
else
print N'ManagerID is ' + #managerID;
go
You should use the return value for status codes only. To return data, you should use output parameters.
If you want to return a dataset, then use an output parameter of type cursor.
more on RETURN statement
Use this code, Working properly
CREATE PROCEDURE [dbo].[sp_delete_item]
#ItemId int = 0
#status bit OUT
AS
Begin
DECLARE #cnt int;
DECLARE #status int =0;
SET NOCOUNT OFF
SELECT #cnt =COUNT(Id) from ItemTransaction where ItemId = #ItemId
if(#cnt = 1)
Begin
return #status;
End
else
Begin
SET #status =1;
return #status;
End
END
Execute SP
DECLARE #statuss bit;
EXECUTE [dbo].[sp_delete_item] 6, #statuss output;
PRINT #statuss;
With the Return statement from the proc, I needed to assign the temp variable and pass it to another stored procedure. The value was getting assigned fine but when passing it as a parameter, it lost the value. I had to create a temp table and set the variable from the table (SQL 2008)
From this:
declare #anID int
exec #anID = dbo.StoredProc_Fetch #ID, #anotherID, #finalID
exec dbo.ADifferentStoredProc #anID (no value here)
To this:
declare #t table(id int)
declare #anID int
insert into #t exec dbo.StoredProc_Fetch #ID, #anotherID, #finalID
set #anID= (select Top 1 * from #t)

How to assign an exec result to a sql variable?

How do you assign the result of an exec call to a variable in SQL? I have a stored proc called up_GetBusinessDay, which returns a single date.
Can you do something like this:
exec #PreviousBusinessDay = dbo.up_GetBusinessDay #Date, -1
I always use the return value to pass back error status. If you need to pass back one value I'd use an output parameter.
sample stored procedure, with an OUTPUT parameter:
CREATE PROCEDURE YourStoredProcedure
(
#Param1 int
,#Param2 varchar(5)
,#Param3 datetime OUTPUT
)
AS
IF ISNULL(#Param1,0)>5
BEGIN
SET #Param3=GETDATE()
END
ELSE
BEGIN
SET #Param3='1/1/2010'
END
RETURN 0
GO
call to the stored procedure, with an OUTPUT parameter:
DECLARE #OutputParameter datetime
,#ReturnValue int
EXEC #ReturnValue=YourStoredProcedure 1,null, #OutputParameter OUTPUT
PRINT #ReturnValue
PRINT CONVERT(char(23),#OutputParameter ,121)
OUTPUT:
0
2010-01-01 00:00:00.000
This will work if you wish to simply return an integer:
DECLARE #ResultForPos INT
EXEC #ResultForPos = storedprocedureName 'InputParameter'
SELECT #ResultForPos
declare #EventId int
CREATE TABLE #EventId (EventId int)
insert into #EventId exec rptInputEventId
set #EventId = (select * from #EventId)
drop table #EventId
From the documentation (assuming that you use SQL-Server):
USE AdventureWorks;
GO
DECLARE #returnstatus nvarchar(15);
SET #returnstatus = NULL;
EXEC #returnstatus = dbo.ufnGetSalesOrderStatusText #Status = 2;
PRINT #returnstatus;
GO
So yes, it should work that way.
I had the same question. While there are good answers here I decided to create a table-valued function. With a table (or scalar) valued function you don't have to change your stored proc. I simply did a select from the table-valued function. Note that the parameter (MyParameter is optional).
CREATE FUNCTION [dbo].[MyDateFunction]
(#MyParameter varchar(max))
RETURNS TABLE
AS
RETURN
(
--- Query your table or view or whatever and select the results.
SELECT DateValue FROM MyTable WHERE ID = #MyParameter;
)
To assign to your variable you simply can do something like:
Declare #MyDate datetime;
SET #MyDate = (SELECT DateValue FROM MyDateFunction(#MyParameter));
You can also use a scalar valued function:
CREATE FUNCTION TestDateFunction()
RETURNS datetime
BEGIN
RETURN (SELECT GetDate());
END
Then you can simply do
Declare #MyDate datetime;
SET #MyDate = (Select dbo.TestDateFunction());
SELECT #MyDate;
Here is solution for dynamic queries.
For example if you have more tables with different suffix:
dbo.SOMETHINGTABLE_ONE, dbo.SOMETHINGTABLE_TWO
Code:
DECLARE #INDEX AS NVARCHAR(20)
DECLARE #CheckVALUE AS NVARCHAR(max) = 'SELECT COUNT(SOMETHING) FROM
dbo.SOMETHINGTABLE_'+#INDEX+''
DECLARE #tempTable Table (TempVALUE int)
DECLARE #RESULTVAL INT
INSERT INTO #tempTable
EXEC sp_executesql #CheckVALUE
SET #RESULTVAL = (SELECT * FROM #tempTable)
DELETE #tempTable
SELECT #RESULTVAL
You can use a Table Variable for that
Code:
DECLARE #PreviousBusinessDay DATETIME
DECLARE #Temp TABLE(BusinessDay DATETIME)
INSERT INTO #Temp EXEC dbo.up_GetBusinessDay #Date, -1
SET #PreviousBusinessDay = (SELECT * FROM #Temp)
SELECT #PreviousBusinessDay
https://www.sqlservertutorial.net/sql-server-user-defined-functions/sql-server-table-variables/