Call Stored Procedure Inside Another Stored Procedure

SQL query example, how to call a stored procedure inside another stored procedure in SQL Server.

Here is an example of how to call a stored procedure inside another stored procedure. This is also known as nested stored procedures in SQL Server.
 
Step 1: Create two simple stored procedure to insert some data into two different tables. 
  1. usp_insert_into_Log1 to insert data into tbl_log1  
  2. usp_insert_into_Log2 to insert data into tbl_log2  
  3. both accept four parameters to insert the data.  
Step 2: Here is a requirement that if less then 50000 rows filled in tbl_log1 then insert data into tbl_log1, otherwise another table tbl_log2.
 
Here is an example of how to call a stored procedure within another stored procedure.
  1. CREATE PROCEDURE [dbo].[usp_insert]  
  2. (  
  3. @a varchar(50),  
  4. @b varchar(15),  
  5. @c varchar(6),  
  6. @d varchar(50)  
  7. )  
  8. AS  
  9. BEGIN  
  10. if ((select count(*) from tbl_Log1) <50000)  
  11. exec [dbo].[usp_insert_into_Log1] @a,@b,@c,@d  
  12. else  
  13. exec [dbo].[usp_insert_into_Log2] @a,@b,@c,@d  
  14. END  
Here is a detailed article on this topic: Executing Stored Procedure Within Another Stored Procedure
 
Learn more here: