In a stored procedure, is setting something a parameter and declaring something different?
For example: if I needed to make something a parameter which would be right or wrong?
Alter Prodedure dbo.users_checkin
#userid INT
AS
Begin
OR
Alter Proc
Begin
Declare #userid INT
Declare #date1 DATE
Thank you!
Related
I've inherited an SSRS report which I need some assistance in understanding how the scalar variable works as I just don't get it.
The stored procedure starts off as below and there is no issue running the stored procedure with the #FromPeriod and #ToPeriod.
CREATE PROCEDURE [dbo].[_SP_STOREDPROCEDURE]
(#FromPeriod int, #ToPeriod Int)
AS
set nocount on
declare #v_FromPeriod int
declare #v_ToPeriod int
declare #cnt int, #periods int
declare #v_periodbegindate datetime
declare #v_YearBegindate datetime
ETC. ETC.
Next, I then have the other script which is run on an adhoc basis and this is the part I need help on:
declare #Period int
declare #PriorPeriod int
select #period = cur_per from tbm_Parms
if right(#Period, 2) <> '01'
begin
set #PriorPeriod = #Period - 1
end
exec [_SP_STOREDPROCEDURE] #PriorPeriod, #PriorPeriod
How does this work?
My stored procedure has #FromPeriod and #ToPeriod defined, yet using the adhoc script it is passing #PriorPeriod instead. It works perfectly but I don't understand how/why it works.
Any pearls of wisdom would be appreciated.
Thanks
Your stored procedure "[dbo].[_SP_STOREDPROCEDURE]" simply applies the same value to both parameters.
Your parameters are:
#FromPeriod int, #ToPeriod Int
You invoke with:
exec [_SP_STOREDPROCEDURE] #PriorPeriod, #PriorPeriod
Assuming that #PriorPeriod = 5, that is just the same as:
exec [_SP_STOREDPROCEDURE] 5, 5.
Now #FromPeriod contains 5. And #ToPeriod /also/ contains 5.
But you haven't given us enough code from _SP_STOREDPROCEDURE to explain why, according to you, that works perfectly. But I'm guessing that the answer is in the WHERE clause in your stored procedure.
I am trying to assign result of a stored procedure executed dynamically into a variable. However when i call them, sql throw me this error:
Could not find the stored procedure 'exec dbo.cont_com 3611,892;'
Below is the code snippet I am using:
declare #procedure_to_call varchar(max);
declare #procedure_result int;
set #procedure_to_call='exec dbo.cont_com 3611,892;'
exec #procedure_result=#procedure_to_call;
When I put parenthesis and execute the stored procedure, it works fine:
exec (#procedure_to_call);
But i need to save the return of the #procedure_to_call inside a variable.
This is the sample code for dbo.cont_com SP:
CREATE PROCEDURE dbo.cont_com
(
#id_c int,
#id_f int)
AS
BEGIN
declare #porcent int;
select #porcent=p.porcent
from porcent_table p
where p.id_c=#id_c
and p.id_c=#id_f;
select porcent=#porcent;
return #porcent;
END
Any help will be greatly appreciated.
Try something like this.
DECLARE #procedure_to_call NVARCHAR(MAX) = 'EXEC #x = dbo.cont_com 3611,892'
DECLARE #procedure_result INT;
SET #procedure_result = 0;
EXEC sp_executesql #procedure_to_call, N'#x INT OUT', #procedure_result OUT;
SELECT #procedure_result
It worked for me, hope it work for you.
set #procedure_to_call='exec dbo.cont_com 3611,892;'
exec #procedure_result=#procedure_to_call;
This becomes
exec exec dbo.cont_com 3611,892; -- hence the error
exec (#procedure_to_call) gets around this by exectuing what's in the parentheses as its own activity
Try:
set #procedure_to_call='dbo.cont_com 3611,892;'
How can i execute a stored procedure in another stored procedure in SQL server?
How will I pass the parameters of the second procedure.?
If you only want to perform some specific operations by your second SP and do not require values back from the SP then simply do:
Exec secondSPName #anyparams
Else, if you need values returned by your second SP inside your first one, then create a temporary table variable with equal numbers of columns and with same definition of column return by second SP. Then you can get these values in first SP as:
Insert into #tep_table
Exec secondSPName #anyparams
Update:
To pass parameter to second sp, do this:
Declare #id ID_Column_datatype
Set #id=(Select id from table_1 Where yourconditions)
Exec secondSPName #id
Update 2:
Suppose your second sp returns Id and Name where type of id is int and name is of varchar(64) type.
now, if you want to select these values in first sp then create a temporary table variable and insert values into it:
Declare #tep_table table
(
Id int,
Name varchar(64)
)
Insert into #tep_table
Exec secondSP
Select * From #tep_table
This will return you the values returned by second SP.
Hope, this clear all your doubts.
Suppose you have one stored procedure like this
First stored procedure:
Create PROCEDURE LoginId
#UserName nvarchar(200),
#Password nvarchar(200)
AS
BEGIN
DECLARE #loginID int
SELECT #loginID = LoginId
FROM UserLogin
WHERE UserName = #UserName AND Password = #Password
return #loginID
END
Now you want to call this procedure from another stored procedure like as below
Second stored procedure
Create PROCEDURE Emprecord
#UserName nvarchar(200),
#Password nvarchar(200),
#Email nvarchar(200),
#IsAdmin bit,
#EmpName nvarchar(200),
#EmpLastName nvarchar(200),
#EmpAddress nvarchar(200),
#EmpContactNo nvarchar(150),
#EmpCompanyName nvarchar(200)
AS
BEGIN
INSERT INTO UserLogin VALUES(#UserName,#Password,#Email,#IsAdmin)
DECLARE #EmpLoginid int
**exec #EmpLoginid= LoginId #UserName,#Password**
INSERT INTO tblEmployee VALUES(#EmpName,#EmpLastName,#EmpAddress,#EmpContactNo,#EmpCompanyName,#EmpLoginid)
END
As you seen above, we can call one stored procedure from another
Yes, you can do that like this:
BEGIN
DECLARE #Results TABLE (Tid INT PRIMARY KEY);
INSERT #Results
EXEC Procedure2 [parameters];
SET #total 1;
END
SELECT #total
Your sp_test: Return fullname
USE [MY_DB]
GO
IF (OBJECT_ID('[dbo].[sp_test]', 'P') IS NOT NULL)
DROP PROCEDURE [dbo].sp_test;
GO
CREATE PROCEDURE [dbo].sp_test
#name VARCHAR(20),
#last_name VARCHAR(30),
#full_name VARCHAR(50) OUTPUT
AS
SET #full_name = #name + #last_name;
GO
In your sp_main
...
DECLARE #my_name VARCHAR(20);
DECLARE #my_last_name VARCHAR(30);
DECLARE #my_full_name VARCHAR(50);
...
EXEC sp_test #my_name, #my_last_name, #my_full_name OUTPUT;
...
You can call User-defined Functions in a stored procedure alternately
this may solve your problem to call stored procedure
Yes ,
Its easy to way we call the function inside the store procedure.
for e.g. create user define Age function and use in select query.
select dbo.GetRegAge(R.DateOfBirth, r.RegistrationDate) as Age,R.DateOfBirth,r.RegistrationDate from T_Registration R
Procedure example:
Create PROCEDURE SP_Name
#UserName nvarchar(200),
#Password nvarchar(200)
AS
BEGIN
DECLARE #loginID int
--Statements for this Store Proc
--
--
--
--execute second store procedure
--below line calling sencond Store Procedure Exec is used for execute Store Procedure.
**Exec SP_Name_2 #params** (if any)
END
Assume I have a normal SQL procedure which has a few arguments.
During debugging it would be nice if i could assign some values to these arguments so that I can just highlight the body of the proc and execute it (as opposed to manually replace the variable with values.
Is there any way to do this? I tried:
#Date1 datetime,
#Date2 datetime
SET #Date1 = '2012-03-23'
but it doesn't like it??
Try
DECLARE #Date1 datetime
SET #Date1 = '2012-03-23'
Looks like you were missing the declare statement. If it doesn't like the '2012-03-23' part, you may have to cast it.
If you're going to do this, I suggest you consider adding a #Debug parameter to your procedures:
create procedure dbo.SomeProc #p1 int, #p2 int, #Debug bit = 0x0
as
set nocount on
begin
if #Debug = 0x1 -- set test values only if debugging
begin
print 'Start debugging'
set #p1 = 1
set #p2 = 2
end
/* your code continues here... */
end
Then when you want to test your code, just execute the procedure with #Debug = 0x1 to execute the debugging code.
Put 'declare' word in front of #Date1
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