Automatically Adding Columns to Newly Created Tables

We can examine this topic in more detail later under the name SCHEMA TRIGGERS, but for now I want to talk about a case where there is a one-to-one solution. In the project we are currently working on, we are using GoldenGate to create an exact copy of the database to be used as an ODS. The most important structural difference from the operational database is that every table has two extra columns that store the type of the last operation seen on the row (INSERT, UPDATE) and the time of that operation.

Problem : Even though the GoldenGate parameters were defined so that these two extra columns exist in the target, when a table in the source database goes through DROP and CREATE, or a new table is CREATEd, the table created in the target does not have these two extra columns, and GOLDENGATE throws an error because of the mapping. For a while we got around this problem by manually creating the relevant columns every time such a change occurred, but that can’t go on forever, and considering that GoldenGate was also stopping in the meantime, we needed a more practical solution.

Solution : With an AFTER CREATE ON SCHEMA trigger created at the schema level, after every DDL that runs, if it is a CREATE operation it fires the trigger; inside the trigger we check the ora_dict_obj_type value to see whether it is a table creation, and if it is a create table operation, we add the relevant columns to the table whose name we get from ora_dict_obj_name.

CREATE OR REPLACE TRIGGER ODS.DEF_OT_TS_EKLE AFTER CREATE ON SCHEMA DECLARE l_sysevent varchar2(25); l_Obje_Name varchar2(30); l_Obje_Type varchar2(30); OTekleSQL varchar2(400); TSekleSQL varchar2(400); begin select ora_sysevent into l_sysevent from dual;

select ora_sysevent, ora_dict_obj_name, ora_dict_obj_type into l_sysevent, l_Obje_Name, l_Obje_Type from dual;

if ( l_Obje_Type in (‘TABLE’) ) then TSekleSQL := ‘ALTER TABLE ‘||l_Obje_Name||’ ADD TS TIMESTAMP(6)’; OTekleSQL := ‘ALTER TABLE ‘||l_Obje_Name||’ ADD OT VARCHAR2(20 BYTE)’; EXECUTE IMMEDIATE TSekleSQL; EXECUTE IMMEDIATE OTekleSQL; end if; end;

Adnan Ersöz29 June 2012
The source of this content is https://aersoz.wordpress.com.
© 2026 KDS Data. All rights reserved.