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.
- usp_insert_into_Log1 to insert data into tbl_log1
- usp_insert_into_Log2 to insert data into tbl_log2
- 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.
- CREATE PROCEDURE [dbo].[usp_insert]
- (
- @a varchar(50),
- @b varchar(15),
- @c varchar(6),
- @d varchar(50)
- )
- AS
- BEGIN
- if ((select count(*) from tbl_Log1) <50000)
- exec [dbo].[usp_insert_into_Log1] @a,@b,@c,@d
- else
- exec [dbo].[usp_insert_into_Log2] @a,@b,@c,@d
- END
Learn more here: