Use of "CREATE TRIGGER" "AFTER ALTER ON SCHEMA"
845497Apr 17 2011 — edited Apr 18 2011I need the ability to work on new columns that are added to tables within the same transaction as the script that adds the column. My guess is the best way to do this is with the "AFTER ALTER ON SCHEMA" option of the "CREATE TRIGGER" statement. What I am finding however is that the trigger does not detect the newly added column which is odd given I am using the "AFTER" option and not the "BEFORE" option.
Below is a sample use case (note this is not the SQL I will use in production, but dows demonstarte the problem) ...
CREATE OR REPLACE TRIGGER list_columns
AFTER ALTER ON SCHEMA
DECLARE
cursor get_data is
SELECT *
FROM user_tab_columns
WHERE table_name = ora_dict_obj_name;
BEGIN
DBMS_OUTPUT.ENABLE(200000);
FOR arec IN get_data LOOP
dbms_output.put_line ('The columns are '||arec.column_name);
END LOOP;
END;
/
Trigger created.
set serverout on
create table foo (bar number);
Table created.
alter table foo add (bar_new number);
The columns are BAR
Table altered.
desc foo
Name Type
---------------- -------
BAR NUMBER
BAR_NEW NUMBER
Given this trigger is meant to fire AFTER the ALTER TABLE statement completes (i.e. after the column actually has been added to the table), I would have thought that when I issued the statement "alter table foo add (bar_new number);", I would have 2 rows returned ... "The columns are BAR" AND "The columns are BAR_NEW", however as the use case shows, I only get the original row listed.
Am I missing something? Is there a better way to achieve this?