I have 3 insert stored procedures each SP inserts data in 2 different tables
Table 1 Table 2 idPerson idProduct name productName phoneNumber productdescription FK-idProduct
SP for table 1 SP for table 2
create procedure test1 create procedure test2 WITH WITH EXECUTE as caller EXECUTE as caller AS AS declare declare @idPerson int, @idProduct int, @name varchar(20), @productName varchar(50), @phone varchar(20) @productoDescription varchar(50) SET nocount on; SET nocount on; Begin Begin insert into table1( insert into table2( idPerson, idProduct, name, productName, phone) productDescription) values( values( @idPerson, @idProduct, @name, @productName, @phone) @productDescription) end end
I need to call stored procedure test 2 from stored procedure test 1 and insert the FK-ID in the table 1
In large database applications, it is common to call one stored procedure from another stored procedure. In this blog, I will explain how to execute a stored procedure within another stored procedure in SQL Server. Let's start with creating a stored procedure.
In releases earlier than SQL Server 2000, you can call one stored procedure from another and return a set of records by creating a temporary table into which the called stored procedure (B) can insert its results or by exploring the use of CURSOR variables.
If you are trying to call the procedure get_manager_details inside test_procedure then you first need to create the test procedure. Add create or replace procedure test_procedure . Then after creating the test_procedure you can execute it in an anonymous block which will call the get_manager_details procedure.
Expand the database that you want, expand Programmability, and then expand Stored Procedures. Right-click the user-defined stored procedure that you want and select Execute Stored Procedure. In the Execute Procedure dialog box, specify a value for each parameter and whether it should pass a null value.
Simply call test2
from test1
like:
EXEC test2 @newId, @prod, @desc;
Make sure to get @id
using SCOPE_IDENTITY(), which gets the last identity value inserted into an identity column in the same scope:
SELECT @newId = SCOPE_IDENTITY()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With