HI WELCOME TO KANSIRIS

SQL Server Execute Stored Procedure from Another Stored Procedure with Parameters

Leave a Comment
To call stored procedure from another stored procedure with parameters in sql server follow below steps

Create First Stored Procedure


--- First Stored Procedure
CREATE PROCEDURE SAMPLE1
@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,'Suresh','Chennai'),
(2,'Rohini','Chennai'),
(3,'Sateesh','Vizag')
SELECT * FROM @temp1 WHERE Id=@tempid and Name=@tempname
END

Create Second Stored Procedure to execute first procedure


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

If you observe above query we are calling first procedure using “EXEC SAMPLE1 @tempid=@id,@tempname=@name” and sending parameters to first stored procedure using @tempid,@tempname. Here we need to use same parameter names whatever we mentioned in first procedure to send values otherwise it will throw error.

Now execute second procedure using following query it will automatically execute or call first procedure and return result


-- Execute Second Procedure By Passing Paramters
EXEC SAMPLE2 @id=1, @name='suresh'

If we execute above query we will get output like as shown below

Output


SQL Server Execute Stored Procedure from Another Stored Procedure with Parameters
I hope it helps you to call stored procedure from another stored procedure with parameters. 

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.