I would like to make a cascade copy. I have a MODEL and the specification of this MODEL is stored in 4 tables and one MODEL is identificated by MODEL_ID in the tables. The MODEL_ID of the MODEL is primary key and the MODEL_ID is increased one by adding new MODEL.

How can I make the MODEL's copy?

Thanks.

有帮助吗?

解决方案

Assuming you have to copy a model identified by MODEL_ID=old_model_id first of all you have to create a new MODEL_ID (for example stored in a variable called new_model_id), for example using a sequence (called SEQ_ID_MODEL in below code, but you could use another technique to create a new ID).

Then you have to write an insert command for each of the 4 tables retrieving the model to copy with the old id (old_model_id) and inserting using the new one(new_model_id):

PROCEDURE P_COPY_MODEL
(
  old_model_id  IN NUMBER
)IS

new_model_id number;

begin
    select SEQ_ID_MODEL.nextval into new_model_id from dual;

    insert into table1(MODEL_ID, COLUMN_1, COLUMN_2, ..., COLUMN_N) 
    select (new_model_id, COLUMN_1, COLUMN_2, ..., COLUMN_N)
    from table1 where MODEL_ID = old_model_id;

    insert into table2(MODEL_ID, COLUMN_1, COLUMN_2, ..., COLUMN_M) 
    select (new_model_id, COLUMN_1, COLUMN_2, ..., COLUMN_M)
    from table2 where MODEL_ID = old_model_id;

    --and so on for table3 and table4

end;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top