How can i execute a stored procedure in another stored procedure in SQL server? How will I pass the parameters of the second procedure.?
-
Are you planning to do this in C# or as a StoredProc on the Server itself? – MethodMan Apr 04 '13 at 04:27
-
use the `exec` keyword. – Matthew Apr 04 '13 at 04:27
-
I need to execute in Server itself – Roys Apr 04 '13 at 06:34
-
3Possible duplicate of ... http://stackoverflow.com/questions/8858620/calling-stored-procedure-from-another-stored-procedure-sql-server – SteveC Sep 04 '13 at 08:08
7 Answers
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.

- 487
- 2
- 5
- 17

- 2,500
- 15
- 15
-
tanx.. ya i need values returned by second sp inside the first one. bt the parameter which i need to pass is an output of first procedure. is it possible.? Pls see this example. SP1 Select id from table_1. is it possible to pass this id as the parameter of the second procedure.? – Roys Apr 04 '13 at 06:46
-
Yes this is posible but first you need to store this id into a variable and then pass the same to second sp. See updated answer. – Ken Clark Apr 04 '13 at 07:03
-
The above steps worked.tanx. How can i get the output value of second Sp from the first one..? – Roys Apr 04 '13 at 09:14
-
This is already answered. anyway, see updated answer again for more explaination. – Ken Clark Apr 04 '13 at 09:36
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

- 732,580
- 175
- 1,330
- 1,459

- 1,677
- 13
- 24
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

- 17,013
- 3
- 59
- 105
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;
...

- 4,633
- 3
- 49
- 76
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

- 7,012
- 13
- 42
- 65
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

- 4,052
- 4
- 37
- 42

- 11
- 1