0

I want to insert a record in some tables using a simple stored procedure.My question is: do i have to write several inserting procedures (one for each table) or is it possible that i can pass as a variable the name of the table in which i want to insert the reccord?i've tried to do the latter but it didn't work...is there any way? or should i write one procedure for each table?

4
Contributors
6
Replies
7
Views
5 Years
Discussion Span
Last Post by libathos
0

Considering each table has a different range of columns and data types a stored procedure for each is probably required. If you could use the same insert procedure to insert the same data into several tables that begs the question of why you have these others tables in the first place.

0

Hello,

No you don;t have to have a separate procedure for each and (sorry hericles) I can think of a hundred reasons to insert into multiple tables from a single record. a simple example would be adding an item to inventory that is a part of another item and you have to create the accounting records at the same time.

But I digress, basically all you have to do is treat each insert as a separate query and then put them together after each one works. Below is a query I used to link an unlimited number or items to other items. It will probably not make a lot of sense since you don't have the original tables but I was all part of a procedure called by the insert trigger. SID (server ID), CID (Customer ID), SLAB (Server Label), RID (Rack ID) were some of the parameters passed into the procedure. SOID was the server Object ID. Trust me you don;t want to know what we did but the example is what you are trying to do.

begin

declare SOID INT;

declare ROID INT;

declare COID INT;

declare PSUOID INT;

declare PSUPOID INT;

declare NICOID INT;

declare NICPOID INT;

insert into objects (`object_type_id`, `device_object_id`, `label`, `date_created`)

values(1 , SID, SLAB, now());

set SOID=Last_Insert_ID();

SELECT ObjIDClient.object_auto_id

FROM ObjIDClient

WHERE ObjIDClient.client_id = CID into COID;

SELECT ObjIDRack.object_auto_id

FROM ObjIDRack

WHERE ObjIDRack.rack_id =  RID into ROID;

insert into objects (`object_type_id`, `device_object_id`, `label`, `date_created`)

values(26 , 0, 'PSU', now());

set PSUOID=Last_Insert_ID();

insert into objects (`object_type_id`, `device_object_id`, `label`, `date_created`)

values(43 , 0, 'PSU Power Port', now());

set PSUPOID=Last_Insert_ID();

insert into objects (`object_type_id`, `device_object_id`, `label`, `date_created`)

values(16 , 0, 'NIC', now());

set NICOID=Last_Insert_ID();

insert into`object_objects` (`parent_object_id` ,  `object_id`)

values(ROID, SOID),(COID, SOID),(SOID,PSUOID),(SOID,NICOID),(PSUOID,PSUPOID),(20127,PSUPOID);

end
0

Ok i got your point maybe i wasn't clear enough though.What i want to do, is have one stored procedure which will insert data into only one table.This table should be passed into the procedure as a parameter and what i have to do there, is select some fields of this table a(ctually yes i wasn't clear enough at all,my apologies..).The actual problem is that i can't use the name of the table that was passed as a parameter in the from clause of my select.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.