Results 1 to 5 of 5

Thread: Copying Rows

  1. #1
    Join Date
    Nov 2004
    Posts
    1

    Unanswered: Copying Rows

    Hi, I'm trying to do something very simple (or so I think).

    Let's say I have 2 tables, one is a "shadow" of another.

    CREATE TABLE ORIGINAL (N NUMBER);
    CREATE TABLE SHADOW (M NUMBER);

    Now I want to write a trigger for ORIGINAL so that whenever a row
    is inserted, the trigger will add the SAME row to SHADOW as well.

    CREATE TRIGGER TRIG
    AFTER INSERT ON ORIGINAL FOR EACH ROW
    BEGIN
    INSERT INTO SHADOW :NEW; -- ERROR, this will not work
    END;

    I want to avoid writing "INSERT INTO SHADOW VALUES(:NEW.N);"
    because I have 50+ of these table pairs and they have different
    columns.

    Is there a simple way to say "insert this row into that shadow table"?
    Both the original and the shadow tables have exactly the same columns.

    Thanks.

    P.S. I'm using Oracle 8.1.7.

  2. #2
    Join Date
    Aug 2003
    Location
    Where the Surf Meets the Turf @Del Mar, CA
    Posts
    7,776
    Provided Answers: 1
    What, if anything, do you want to occur when a DELETE or UPDATE is done against ORIGINAL?
    You can lead some folks to knowledge, but you can not make them think.
    The average person thinks he's above average!
    For most folks, they don't know, what they don't know.
    Good judgement comes from experience. Experience comes from bad judgement.

  3. #3
    Join Date
    Jan 2004
    Location
    Croatia, Europe
    Posts
    4,094
    Provided Answers: 4
    Quote Originally Posted by TangentZ
    INSERT INTO SHADOW :NEW; -- ERROR, this will not work
    Should be INSERT INTO shadow VALUES (:new.n);

    And what about Anacedent's question? Or do you ONLY insert data into those tables?

  4. #4
    Join Date
    Apr 2004
    Location
    Toronto, Canada
    Posts
    249
    Assuming you know what you are doing when creating 50 duplicate tables (most likely for historical records), the best approach is to use a code generator.

    Write a small program that accepts the original table name and the copy table name as parameters, and outputs the trigger statement that you need. Use the data dictionary to get the column names of the original table.

    The output would look like:

    Code:
     
    CREATE TRIGGER <TRIG>
    AFTER INSERT ON <ORIGINAL> FOR EACH ROW
    BEGIN
    INSERT INTO <SHADOW> ( <col1>, <col2>, ...)
    values (:new.<col1>, :new.<col2>, ....); 
    END;
    /
    Either save this output to a file and execute it, or use dynamic sql to execute this block of code.

    Hope that helps.

    Ravi

  5. #5
    Join Date
    Jun 2004
    Location
    Liverpool, NY USA
    Posts
    2,509
    What are these shadow files used for. If all they are is a copy of the original table, this can be done a couple of ways.

    1) Set up a materilized view.
    2) if you just have different applications that know the data under different names, then use synonyms to have multiple names for the same table.
    Bill
    You do not need a parachute to skydive. You only need a parachute to skydive twice.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •