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

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

Related

Parameter not set still passing value in stored procedure sql

ALTER PROCEDURE [dbo].[XXXXX]
#type varchar(20)
,#Name varchar(20)
,#DayVal int
,#MonthVal int
,#YearVal int
AS
BEGIN
If type='yyy'
begin
insert into dbo.destinationDB
(name,Dayval,Monthval,Yearval)
values
(#name,#DayVal,#MonthVal,#YearVal)
In the above Stored procedure of SQL Server the parameters #name, #DataVal, #MonthVal, #YearVal is not defined anywhere neither the values are set in any other Stored Procedures or Jobs . But it still passing values and values are updated in the destination table.
If there is another procedure or trigger calling your proc you will find it in the sys.sql_module:
SELECT OBJECT_NAME(object_id), *
FROM sys.sql_modules
WHERE definition LIKE '%XXXX%' -- your proc name
If it is not there there are at least 3 possible scenarios:
there is a linked server calling your server
there is an external application
someone else is calling it from ssms
You're probably comparing with null, try adding and type IS NOT NULL

Get value from Another Stored procedure(B) in a stored Procedure(A)

I have a stored procedure ssspAccProfitAndLoss which returns result as shown below:
I have another stored procedure named ssspAccBalanceSheet. In this store procedure i have a variable declared as
Declare totalProfitAndLoss decimal(18,0)
I want the Total sum of Amount1 column of ssspAccProfitAndLoss and set it to totalProfitAndLoss. How can i achieve this.
Set totalProfitAndLoss = Select Sum(Amount1) from ssspAccProfitAndLoss
Thanxxxxx in advance....
If you don't want to change the ssspAccProfitAndLoss procedure you probably need to work through variable table - if the ssspAccProfitAndLoss is short running SP than it's probably acceptable.
declare #totalProfitAndLoss decimal(18,0); --I'd go for BIGINT here unless you're sure you need 18 digits
declare #resultTable table (
Particular varchar(100),
Amount decimal(18,0),
Particular2 varchar(100),
Amount2 decimal(18,0));
insert into #resultTable exec ssspAccProfitAndLoss
select #totalProfitAndLoss = sum(Amount1) from #resultTable

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.

Stored Procedure Output

I've got two stored procedures:
SP1
CREATE PROCEDURE GetAge
#Birthday datetime,
#BirthDayAge INT OUTPUT
AS
SELECT #BirthDayAge = YEAR(GETDATE()-DATEPART(dy, #Birthday) + 1)-YEAR(#Birthday);
SP2
CREATE PROCEDURE AgeProc
#Age int
AS
DECLARE #BirthDayAge INT;
EXEC GetAge #Age, #BirthDayAge OUTPUT
SELECT ... FROM ... WHERE #BirthdayAge = #Age;
For some reason or other, no results are being returned in the second procedure when tested. Am I doing something wrong in either of the stored procedures?
WHERE #BirthdayAge = #Age;
you are comparing 2 variables.
Shouldnt one of this be a table column?
also, you are passing an integer to a datetime, that may cause issues

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