Below I'm using simple example, real tables are bigger, I cannot store devices in one table, I can not merge device1 and device2 tables.
I have three tables: device1, device2, computer(device3), device1 and device2 have id from the same sequence, device3 is computer. Table device3 can be connected with device1 and device2 (many-to-many), so I have two tables with relations: rel1with3 and rel2with3.
CREATE SEQUENCE device_1or2;
CREATE SEQUENCE device_3;
CREATE TABLE device1
( d_id NUMBER, d_data VARCHAR2(20)
, CONSTRAINT device1_pk PRIMARY KEY (d_id));
CREATE TABLE device2
( d_id NUMBER, d_data VARCHAR2(20)
, CONSTRAINT device2_pk PRIMARY KEY (d_id));
CREATE TABLE computer
( d_id NUMBER, d_data VARCHAR2(20)
, CONSTRAINT device3_pk PRIMARY KEY (d_id));
CREATE TABLE rel1with3
( d_id_1 NUMBER, d_id_3 NUMBER
, CONSTRAINT rel13_fk_1 FOREIGN KEY (d_id_1) REFERENCES device1 (d_id)
, CONSTRAINT rel13_fk_3 FOREIGN KEY (d_id_3) REFERENCES computer (d_id)
);
CREATE UNIQUE INDEX I_REL_13 ON rel1with3 (d_id_1, d_id_3);
CREATE TABLE rel2with3
( d_id_2 NUMBER, d_id_3 NUMBER
, CONSTRAINT rel23_fk_2 FOREIGN KEY (d_id_2) REFERENCES device2 (d_id)
, CONSTRAINT rel23_fk_3 FOREIGN KEY (d_id_3) REFERENCES computer (d_id)
);
CREATE UNIQUE INDEX I_REL_23 ON rel2with3 (d_id_2, d_id_3);
I want to create stored procedure which have two in parameters (device1 or device2 as first parameter, computer as second) and add/remove relation (insert/delete row in relation table).
CREATE OR REPLACE PROCEDURE Add_Relation
( COMPUTER NUMBER
, DEVICE NUMBER
, ERR OUT NUMBER
) IS
PRAGMA AUTONOMOUS_TRANSACTION;
device_data VARCHAR2(20);
BEGIN
-- detect which device it is, insert record
-- stored procedure, the same for both device type, BUT uses device data from additional table field
WRITE_LOG(device, device_data, computer);
COMMIT;
EXCEPTION
WHEN OTHERS THEN ERR := TO_CHAR(SQLCODE);
END;
Question is: HOW better organize this? Using another function? Using IF-ELSE? How is faster for execute etc?