2

We are trying to copy the current row of a table to mirror table by using a trigger before delete / update. Below is the working query

 BEFORE UPDATE OR DELETE 
  ON CurrentTable FOR EACH ROW
  BEGIN
      INSERT INTO MirrorTable
 ( EMPFIRSTNAME,
   EMPLASTNAME,
   CELLNO,
   SALARY
    )
VALUES
 ( :old.EMPFIRSTNAME,
   :old.EMPLASTNAME,
   :old.CELLNO,
   :old.SALARY
    ); 
END;

But the problem is we have more than 50 coulmns in the current table and dont want to mention all those column names. Is there a way to select all coulmns like :old.*

SELECT  * INTO MirrorTable FROM CurrentTable

Any suggestions would be helpful.

Thanks,

4

3 回答 3

2

Realistically, no. You'll need to list all the columns.

  • You could, of course, dynamically generate the trigger code pulling the column names from DBA_TAB_COLUMNS. But that is going to be dramatically more work than simply typing in 50 column names.
  • If your table happens to be an object table, :new would be an instance of that object so you could insert that. But it would be rather rare to have an object table.
于 2012-04-18T18:25:52.997 回答
1

If your 'current' and 'mirror' tables have EXACTLY the same structure you may be able to use something like

INSERT INTO MirrorTable
  SELECT *
    FROM CurrentTable
    WHERE CurrentTable.primary_key_column = :old.primary_key_column

Honestly, I think that this is a poor choice and wouldn't do it, but it's a more-or-less free world and you're free (more or less :-) to make your own choices.

Share and enjoy.

于 2012-04-18T18:37:32.393 回答
0

For what it's worth, I've been writing the same stuff and used this to generate the code:

SQL> set pagesize 0
SQL> select ':old.'||COLUMN_NAME||',' from all_tab_columns where table_name='BIGTABLE' and owner='BOB';
:old.COL1,
:old.COL2,
:old.COL3,
:old.COL4,
:old.COL5,
...

If you feed all columns, no need to mention them twice (and you may use NULL for empty columns):

INSERT INTO bigtable VALUES (
:old.COL1,
:old.COL2,
:old.COL3,
:old.COL4,
:old.COL5,
NULL,
NULL);

people writing tables with that many columns should have no desserts ;-)

于 2013-05-23T18:42:50.347 回答