Calling/Executing SQL Stored Procedure from Another Stored Procedure

In this blog I will explain one of the major application in stored procedure that will execute one procedure from another procedure with parameters.

Let's start with creating stored procedure.

Here's the query for creating the first procedure: 
  1. create procedure Sp_insert  
  2. (  
  3.   
  4. @ID int ,  
  5.   
  6. @TempName varchar(max)  
  7.   
  8. )  
  9.   
  10. as  
  11.   
  12. begin  
  13.   
  14. Declare @SampleTable Table(id intName varchar(max))  
  15.   
  16. Insert into @SampleTable(id,Name)values(@ID,@TempName)  
  17.   
  18. select*from @SampleTable  
  19.   
  20. end  

My Second Stored procedure: 

  1. create procedure Sp_Call  
  2.   
  3. (  
  4.   
  5. @SID int,  
  6.   
  7. @Name varchar(max)  
  8.   
  9. )  
  10.   
  11. as  
  12.   
  13. begin  
  14.   
  15. exec Sp_insert @ID=@SID,@TempName=@Name  
  16.   
  17. end  
  18.   
  19. From t  
In the above query you can notice that we are calling first stored procedure  Sp_insert  and passing the parameter  @ID and @TempName to it.

Now execute the second procedure using the following query. It will call the first procedure and return the result. 
  1. Exec Sp_Call @SID=1,@Name='Arun'  
The result:
 
   
Ebook Download
View all
Learn
View all