SQL Server Execute Stored Procedure from Another Stored Procedure with Parameters

Category > MSSQLSERVER || Published on : Friday, November 6, 2015 || Views: 2256 || SQL Server Execute Stored Procedure from Another Stored Procedure with Parameters


Step 1: Open MS-SQL Server

Step 2: Select a database

Step 3: Create first Stored Procedure with the following codes:-

-- www.sourcecodehub.com ---

 --- First Stored Procedure
CREATE PROCEDURE sp1
@tempid INT,
@tempname varchar(50)
AS
BEGIN
DECLARE @temp1 TABLE (Id INT, Name VARCHAR(50), Location VARCHAR(50))
INSERT INTO @temp1 (Id, Name, Location)
VALUES(1,'raja','india'),
(2,'Rohan','UK'),
(3,'Satu','Nepal')
SELECT * FROM @temp1 WHERE Id=@tempid and Name=@tempname
END

Step 3: Create second Stored Procedure which will call first Stored Prodedure with the following codes:-

-- www.sourcecodehub.com ---
 --- Second Stored Procedure
CREATE PROCEDURE SAMPLE2
@id INT,
@name varchar(50)
AS
BEGIN
-- Calling First Procedure From Second Procedure
EXEC sp1 @tempid=@id, @tempname=@name
END

HOW TO USE:-

EXEC SAMPLE2 @id=1, @name='raja'