Stored Procedure Output - sql

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

Related

How to initialize values to two or more variables by calling another procedure using sql server?

say this procedure have to be called
select #className = Name
from dbo.ClassNames
where id=#classId
select #sectionName = SectionName
from dbo.ClassSections
where id=#sectionId
select #className as 'Class Name',#sectionName as 'Section Name'
the other procedure is:
declare #className nvarchar(50),
#sectionName nvarchar(50)
EXEC [dbo].[testAll]
#regNo=#regNo
so how to assing value to #className and #classSection by calling the above procedure???
create procedures using OUTPUT
https://technet.microsoft.com/en-us/library/ms187004(v=sql.105).aspx
create procedure spOne (#param1 int OUTPUT, #param2 varchar(20) OUTPUT)
as
select #param1= x from table
create procedure spTwo (#param1 int, #param2 varchar(20))
as
select x from table where y=#param1
Declare variables
declare #param1 int
declare #param2 varchar(20)
exec procedure using OUTPUT
exec spOne #param1 OUTPUT, #param2 OUTPUT
Now those variables holds values generated inside spOne
exec spTwo #param1, #param2
I think you missed something. You can't actually use the return of the proc like that or assign a variable in another procedure, they are out of context.
I would solve this in one of the 2 ways:
Define a View returning the 2 fields you need, that way you could filter the results by #regNo and return its results in another procedure, just like a normal table.
Use the return as a OUTPUT field in the procedure. That could be a table or simple 1 of the fields.
Hope it helps...

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

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.

can we have a stored procedure with output parameters and return statement?

Can a stored procedure have output parameters and return statement? If so can anybody give me a simple example.thank you all.
Stored procedure can return integer type only in a return statement and can have any number of out parameters. See this for references supporting this.
Simplest eg of stored procedure
Return integer value from SP
CREATE procedure [sys].[sp_name]
(
#var1 bit = 0,
#publisher smallint
)
AS
BEGIN
IF #var1<> 0
RETURN (#publisher )
END
Using Out Parameter
CREATE PROCEDURE GetImmediateManager
#employeeID INT,
#managerID INT OUTPUT
AS
BEGIN
SELECT #managerID = ManagerID
FROM HumanResources.Employee
WHERE EmployeeID = #employeeID
END
You can use Transactions, Exception handling(try Catch), DDL and DML Queries, calling another stored procedure within one stored procedures and many more operations.
Please comments for more functionalities supported in stored procedure
If you mean the standard RETURN statement that gives an integer then yes
If you mean a UDF RETURN , then no. But a stored proc can have a normal SELECT
You are free to use both OUTPUT params and a single RETURN value:
CREATE PROCEDURE RaiseToPower (
#IN INT,
#OUT INT OUTPUT
)
AS
DECLARE #POWER INT
SET #POWER = 3
SET #OUT = POWER(#IN, #POWER)
RETURN #POWER
GO
/**/
DECLARE #POW INT, #RESULT INT
EXEC #POW = dbo.RaiseToPower 2, #RESULT OUTPUT
SELECT 2, 'raised to', #POW, 'is', #RESULT
>> 2 raised to 3 is 8
No. It is either a stored procedure or a scalar function.
A scalar function returns a value and takes 0 to n in parameters.
A stored procedure can take from 0 to n input parameters and can have 0 to n output parameters.

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