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.
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:
In
previous articles I explained SQL Server insert multiple records with one insert statement,
SQL Server restore database from
.bak / .mdf file, SQL Server single procedure to
insert update delete, SQL Server take database backup, Primary key constraint in sql
server,
foreign key constraint in sql
server,
cursor example in sql server and many articles relating to SQL
server.
Now I will explain how to call or execute stored procedure from another stored
procedure in sql
server
with parameters.
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
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. |
|||
|
|||
3 comments :
Nice one, where it will be used in real time ?
Nice one, where it will be used in real time ?
good this will help me alot
Note: Only a member of this blog may post a comment.