how to SET store procedure output value to a declare variable - sql

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

Related

Get result parameter of stored procedure in exec clause

I have a stored procedure that returns multiple parameters:
CREATE PROCEDURE [dbo].[TestSP]
#Test1 INT
, #Test2 UNIQUEIDENTIFIER
--some inserts and alters here
SELECT TOP 1
#Parameter1 AS Design
, #Parameter2
, #Parameter3
FROM Table
I want to use EXEC into another stored procedure and get ONLY #Parameter1 (Design)
So I want to get #Parameter1 after EXEC stored procedure, so I think about OUTPUT, but it doesn't work, is there a way to achieve this?
CREATE PROCEDURE [dbo].[SecondStoredProcedure]
#Sender1 INT
, #Sender2 UNIQUEIDENTIFIER
DECLARE #ReturnedParameter1 INT
EXEC [dbo].[TestSP] #Test1 = #Sender1, #Test2 = #Sender2 OUTPUT [Design]
INTO #ReturnedParameter1
SELECT #ReturnedParameter1
That procedure creates a resultset, and has no output parameters. You can capture a resultset with insert into ... exec, like this:
use tempdb
go
CREATE PROCEDURE [dbo].[addDesign]
#Test1 INT
,#Test2 UNIQUEIDENTIFIER
as
begin
--some inserts and alters here
SELECT
1 AS Design
, 2 as Foo
, 3 as Bar
end
go
declare #rv table(Design int, Foo int, Bar int)
declare #test2 uniqueidentifier = newid()
insert into #rv
exec addDesign 1, #test2
declare #design int = (select Design from #rv)
select #design
I suggest using an output parameter as explained in the official docs.
Here is how your code might look in this case:
use tempdb
go
create procedure [dbo].[addDesign]
(
#Test1 int
, #Test2 uniqueidentifier
, #Design int out
)
as
begin
set nocount on;
--some inserts and alters here
-- Set the return parameter
set #Design = #Parameter1;
-- Select the return results
SELECT TOP 1
#Parameter1
, #Parameter2
, #Parameter3
FROM dbo.MyTable;
-- Return status code, proc ran OK
return 0;
end
go
create procedure [dbo].[SecondStoredProcedure]
(
#Sender1 int
, #Sender2 uniqueidentifier
)
as
begin
set nocount on;
declare #ReturnedParameter1 int;
exec dbo.TestSP #Test1 = #Sender1, #Test2 = #Sender2, #Design = #ReturnedParameter1;
select #ReturnedParameter1;
-- Return status code, proc ran OK
return 0;
end
go
Note: This demonstrates the 3 ways information can be returned from a stored procedure, the result code (only 1), output parameters (0-N) and result sets (0-N).

How do I use one stored procedure as a "master" so that it invokes another procedure and returns that other procedure's output as its own?

Basically in my "master sproc" I will just check a few of the input parameters to determine which subordinate sproc to call, and then just pass the output of that subordinate sproc back to the caller. Using SQL Server.
Here is code that doesn't work:
CREATE PROCEDURE dbo.master
#a char(1)
, #b char(1)
AS
IF #a > #b
EXEC dbo.proc_a #a, #b;
ELSE
EXEC dbo.proc_b #a, #b;
GO
This code just returns "0", not the results.
T-SQL code such a "master" proc would be like:
CREATE PROCEDURE dbo.master
#a char(1)
, #b char(1)
AS
IF #a > #b
EXEC dbo.proc_a #a, #b;
ELSE
EXEC dbo.proc_b #a, #b;
GO
Result sets returned by the called procedure will be returned to the client just like if run directly from the master proc.
EDIT:
Below is an example using the code from your comment:
CREATE TABLE dbo.TransactionsA (col1 int);
INSERT INTO dbo.TransactionsA VALUES(1);
CREATE TABLE dbo.TransactionsB (col1 int);
INSERT INTO dbo.TransactionsB VALUES(2);
GO
CREATE PROCEDURE [dbo].[GetData]
#A int = 0
, #B int = 0
AS
IF (#A > #B)
EXEC dbo.GetAData #A, #B;
ELSE
EXEC dbo.GetBData #A, #B;
GO
CREATE PROCEDURE [dbo].[GetAData]
#A int = 0
, #B int = 0
AS
SET NOCOUNT ON;
SELECT * FROM TransactionsA;
GO
CREATE PROCEDURE [dbo].[GetBData]
#A int = 0
, #B int = 0
AS
SET NOCOUNT ON;
SELECT * FROM TransactionsB;
GO
EXEC dbo.GetData 1,2; --returns 2
EXEC dbo.GetData 2,1; --returns 1
EXEC dbo.GetData 1,1; --returns 2
GO
I don't know why you are comparing chars but the problem maybe is in you SPs previous definition, I tried the code and is working fine returning data
CREATE PROCEDURE [dbo].[GetData]
#A int = 0,
#B int = 0
AS
BEGIN
IF (#A > #B)
EXEC dbo.GetAData #A, #B;
ELSE
EXEC dbo.GetBData #A, #B;
END
GO
CREATE PROCEDURE [dbo].[GetAData]
#A int = 0,
#B int = 0
AS
BEGIN
SET NOCOUNT ON;
SELECT * FROM [ReportTables].[MyOwnTable1]
END
GO
CREATE PROCEDURE [dbo].[GetBData]
#A int = 0,
#B int = 0
AS BEGIN
SET NOCOUNT ON;
SELECT * FROM [ReportTables].[MyOwnTable2]
END

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

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)

Assign a variable to the output of a stored procedure

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