Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling stored procedure from another stored procedure SQL Server

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

like image 263
arturo r Avatar asked Jan 13 '12 23:01

arturo r


People also ask

Can a stored procedure call another stored procedure SQL Server?

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.

Can I call stored procedure in another 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.

How do you call a procedure from another procedure?

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.

How do you execute a stored procedure in a stored 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.


1 Answers

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() 
like image 173
Mark Kadlec Avatar answered Sep 28 '22 04:09

Mark Kadlec