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

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.

Related

Get first row, first column value from stored procedure

I am calling a stored procedure (that I can't modify) that returns a single value via a SELECT at the end of the procedure. I need to use that value in my own procedure. How can I get that value?
Here is an example of the procedure I am calling that I can not modify:
CREATE PROCEDURE [dbo].[SP_poorlywritten] (
....
)
BEGIN
....
SELECT #lastkey;
RETURN (0);
END
And here is what I am trying to do, but it doesn't work because this gets the return value, and what I need is that SELECT value:
exec #next_key = SP_poorlywritten 'tablename',1;
How can I store the first column, first row value from a store procedure?
If you cannot modify the existing stored procedure, you will not be able to utilize the RETURN as you would like to.
An alternative may be to insert the output of the procedure's select statement into a temp table, and then query that directly to populate a variable.
That would look like this.
CREATE TABLE #tmp
(
LastKey INT
)
INSERT INTO #tmp
EXEC [dbo].[SP_poorlywritten]
See this existing post for more details on how you might accomplish this.
As Aaron Bertrand pointed out, RETURN is for error/status. If you were able to modify the stored procedure, you would want to utilize an output parameter instead of RETURN. This is how you would do that.
CREATE PROCEDURE proc_name
#Output int OUTPUT
AS
<do some stuff>
SET #Output = <some_value>
GO
DECLARE #Output int
EXEC proc_name #Output = #Output

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)

Run one stored procedure within another stored procedure in SQL Server 2008

I have a stored procedure named [usp_Movie_GetUserPaidList] that takes two arguments #MovieID INT, #UserName Nvarchar(250) and returns data something like this
Exec usp_Movie_GetUserPaidList #MovieID, #UserName
IsPaidUser | IsSubscribeUser
0 0
Now in my another stored procedure I have something like
DECLARE #tblTemp1 TABLE (
MovieID INT
,IsPaidUser BIT
,IsSubscribeUser BIT
)
Here I know #MovieID value
Now I need to do something like
INSERT INTO #tblTemp1
SELECT #MovieID (EXEC [usp_Movie_GetUserPaidList] #MovieID,#userName )
Which is obviously not correct.
Help me to do so...Thank you for your time.
You need to take output in another variable and then use it:
DECLARE #Movieid int
EXEC #Movieid = [usp_Movie_GetUserPaidList] #MovieID,#userName
Here assumptions are:
1. You have #MovieID and #userName with you.
2. Your stored proc usp_Movie_GetUserPaidList is returning proper value.
Why don't you return MovieID in the output of stored procedure usp_Movie_GetUserPaidList since you know it there also. Then you can use simple insert in another stored procedure like :
INSERT INTO #tblTemp1
EXEC [usp_Movie_GetUserPaidList] #MovieID,#userName

SQL Server Stored Procedure capture return value in T-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

How to query from a stored procedure in SQL Server?

Let say I have a simple Stored Procedure:
ALTER PROCEDURE [dbo].[myProc]
AS
BEGIN
SELECT * FROM myTable
END
How can I do a WHERE statement in Microsoft SQL Server Management Studio to the stored procedure? Something like that:
SELECT * FROM myProc WHERE x = 'a'; -- But that doesn't work...
It sounds like you're trying to make a "dynamic" stored procedure.
Something you might want to do is:
1) Insert the contents of your stored procedure into a temporary table
2) Use dynamic sql to apply a where condition to that temporary table.
Something like:
declare #as_condition varchar(500); --Your condition
create table #a
(
id bigint
)
insert into #a
execute sproc
declare #ls_sql varchar(max);
set #ls_sql = "select * from #a where " + #as_condition;
execute (#ls_sql);
SQL Server allows you to use INSERT INTO to grab a stored procedure's output. For example, to grab all processes with SPID < 10, use:
create table #sp_who (
spid smallint,
ecid smallint,
status nchar(30),
loginame nchar(128),
hostname nchar(128),
blk char(5),
dbname nchar(128),
cmd nchar(16),
request int)
insert into #sp_who execute sp_who
select * from #sp_who where spid < 10
You can't add a WHERE clause to a stored procedure like this.
You should put the clause in the sproc, like this:
ALTER PROCEDURE [dbo].[myProc]
#X VARCHAR(10)
AS
BEGIN
SELECT * FROM myTable WHERE x=#X
END
GO
The syntax for calling a stored procedure is through the use of EXECUTE not SELECT(e.g.):
EXECUTE dbo.myProc 'a'
I think you can't do that.
The command to execute a stored procedure is EXECUTE.
See some more examples of the EXECUTE usage.
I think its better to use a view or a table valued function rather than the suggested approach. Both allow you to pass parameters to the function
If you want the WHERE clause to be something you can "turn off" you can do this, passing in a predetermined value (e.g. -1) if the WHERE limitation is to be bypassed:
ALTER PROCEDURE [dbo].[myProc]
#X VARCHAR(10)
AS
BEGIN
SELECT * FROM myTable WHERE x=#X or #X = -1
END
GO
You must declare a variable in the store procedure which will be necessary to pass to run the stored procedure. Here is an example. Keep this in mind: Before AS you can simply declare any variable by using the # character, but after the AS you must write Declare to declare any variable, e.g., Declare #name nvarchar (50).
ALTER PROCEDURE [dbo].[myProc]
#name varchar (50)
AS
BEGIN
SELECT * FROM myTable
where name= #name
END