Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

SQL Server Execute Stored Procedure from Another Stored Procedure with Parameters

Aug 13, 2015
Introduction:

Here I will explain how to call or execute one stored procedure from another stored procedure in
sql server or execute stored procedure with parameters from another stored procedure in sql server. To call or execute stored procedure within another stored procedure we need to create stored procedure in sql server and call that procedure like “EXEC SAMPLE1” from another stored procedure.
Description:

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. 

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

3 comments :

Rajkumar Palnati said...

Nice one, where it will be used in real time ?

Rajkumar Palnati said...

Nice one, where it will be used in real time ?

Unknown said...

good this will help me alot

Give your Valuable Comments

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

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.