SQL Server Stored Procedure capture return value in T-SQL - sql

I have a SQL Server stored procedure; I need to capture the return value from the stored procedure. Is this the correct way of doing this?
declare valback varchar(30)
set valback = exec storeproc1
In this case, storeproc1 is my stored procedure.

To start, use proper T-SQL syntax:
declare #valback int;
exec #valback = storeproc1;
The only return type allowed for a stored procedure is int. Stored procedures return status via the return statement.
I somehow have a feeling that you really want something else, namely:
to have an OUTPUT parameter in the procedure:
declare #valback varchar(30);
exec storedproc1 #valback OUTPUT;
or capture the procedure result set via INSERT ... EXEC. See How to Share Data Between Stored Procedures.

The correct syntax is:
DECLARE #valback VARCHAR(30)
EXEC #valback = storeproc1
As per the documentation:
http://msdn.microsoft.com/en-us/library/ms188332.aspx

Related

stored procedure -Print

create procedure [dbo].[basic_und6] (#name varchar(500), #email varchar(500), #result int out)
as
begin
Select #result=COUNT(*) from tbl_Students
where Firstname= #name and Email=#email
print #result
end
Given below is the query which I am running to execute the stored procedure
**declare #result1 int
execute basic_und6 Amit,'amit#abc.com',#result1
print #result1**
I am getting result as 1.
But according to my understanding, it should have displayed two 1's
First 1 is from the stored procedure print and second 1 from the query which had print statement.
Modify your execute statement to this :
declare #result1 int
execute basic_und6 Amit,'amit#abc.com', #result1 OUTPUT
print #result1
This is because, you haven't specified a variable where the output value must be returned to. The calling statement must do this by explicitly using the OUTPUT keyword. More info here
If you specify the OUTPUT keyword for a parameter in the procedure definition, the stored procedure can return the current value of the parameter to the calling program when the stored procedure exits. To save the value of the parameter in a variable that can be used in the calling program, the calling program must use the OUTPUT keyword when executing the stored procedure.
No it will show only one 1
when we select a value into a variable the row count will not be shown

Storing one or several sql statements as a variable in a stored procedure in SQL Server

Let's say I have a stored procedure. Is it possible to store multiple sql statements in a variable inside a stored procedure and later execute it when the procedure is invoked?
Example:
CREATE PROCEDURE dbo.StoredProcedure
(
#parameter1 int = QUERIES_HERE
)
AS
/*something like execute #parameter1*/
RETURN
Yes, like this:
DECLARE #SQLQuery varchar(500)
SET #SQLQuery = 'SELECT * FROM Employees WHERE EmployeeID = 123'
EXECUTE(#SQLQuery)

store the return value from a procedure in a variable

I am trying to execute a procedure(A) inside another procedure(B) and I want to store the value returned from the procedure A in a variable called #LogId.
after executing Procedure A
exec TES_usp_getnextlogid
it returns something like
NewLogId = xxxx
I tried doing the following in procedure B.
declare #LogId int
exec #LogId = TES_usp_getnextlogid
my question is, why doesn't #LogId holds the value which is returned from TES_usp_getnextlogid Procedure?
Thanks.
use output parameter for this, return value should only be used for returning Success/Faliuer status.
CREATE PROCEDURE My_Outter_Proc2
AS
BEGIN
DECLARE #NextID INT;
EXEC TES_usp_getnextlogid #NextID OUTPUT
EXEC My_Inner_Proc #NextID
END

Is there a SQL Server function type that doesn't return anything?

I would like to create a SQL Server function that doesn't return anything but rather simply updates based on some input.
Looking at the documentation, there doesn't seem to be a function like this?
How can I acheive this behaviour?
Basically, I need a function type that I can exec at anytime to do some updating.
Like:
exec UpdateData 'derp'
A stored procedure, is a "type" of function, and it is usually used for situations just like yours, to modify some date when executed.
You can pass it parameters, 'derp', and create some SQL logic to update your records. In the end the stored procedure will just execute and won't return anything (actually, you can return information but it's not the topic of your question).
Create a stored procedure
e.g.
Create Procedure usp_UpdateDate
#Id int,
#Value varchar(100)
AS
Update TableName
Set Data= #Value
Where Id = #Id
Then you can
exec usp_UpdateDate 1,'value to update'

Calling one stored procedure within another stored procedure using variables from first stored procedure

I have a stored procedure say #Create_Dummy1 which is being passed a variable. This is declared as #Dummy_Variable1 in this stored procedure.
Next I need to call another stored procedure #Create_Dummy2 from #Create_Dummy1. I need to pass #Dummy_Variable1 in the exec statement.
But if I try to do this the string #Dummy_Variable1 is only being passed instead of the value it holds.
I'm executing procedures inside other procedures like this:
DECLARE #childResult int, #loaErrorCode int, #loaErrorMessage varchar(255)
EXEC #childResult = [dbo].[proc_sub_getSomething] #schemes_id = #foo_schemes_i, #errorCode = #loaErrorCode OUTPUT , #errorMessage = #loaErrorMessage OUTPUT
Should it still not work you should edit your question to show your exact code.
This should work:
create procedure Create_Dummy1
(
#Dummy_Variable1 int
)
as
exec Create_Dummy2 #Dummy_Variable1
Go
And
create procedure Create_Dummy2
(
#Dummy_Variable1 int
)
as
Select * From yourTable WHERE tableColumn = #Dummy_Variable1
And this is how you call it:
exec Create_Dummy1 1
Hope this helps.