
SET SERVEROUTPUT ON;
DECLARE
  v_ins_str VARCHAR2(4096);
  v_upd_str VARCHAR2(4096);
  v_trigger_sql VARCHAR2(32767);
  v_trigger_name VARCHAR2(128);
  v_ref_table_name VARCHAR2(128);
  v_table_name VARCHAR2(128); -- GIVE VALUE FOR ONE TABLE TRIGGER GENERATION
BEGIN
  FOR i IN (
    SELECT  
      'create or replace TRIGGER REFERENCE.AUTOKEY_'||TABLE_NAME||' 
BEFORE INSERT ON REFERENCE.'||TABLE_NAME||' 
FOR EACH ROW 
WHEN (NEW.A_KEY IS NULL) 
BEGIN  
  SELECT 
    NVL(MAX(A_KEY), 0) + 1 
  INTO :NEW.A_KEY  
  FROM REFERENCE.'||TABLE_NAME||'; 
END;

'  AS trigger_sql,
'REFERENCE.AUTOKEY_'||TABLE_NAME AS trigger_name
    FROM DBA_TABLES t 
    WHERE 
        OWNER = 'REFERENCE' 
    AND TABLE_NAME IN (
'T_BALANCE_SHEET_STRUCTURE', 'T_CAPITAL_KEY', 'T_COUNTRY', 'T_CSDB_RATING_ACTION', 'T_CURRENCY', 'T_TMS_ACTIVITY_GROUPING'
    ) 
    ORDER BY TABLE_NAME
  ) LOOP
    v_trigger_sql := i.trigger_sql;
    
  DBMS_OUTPUT.PUT_LINE(i.trigger_name);
  EXECUTE IMMEDIATE v_trigger_sql;

--  DBMS_OUTPUT.PUT_LINE(v_trigger_sql||'
--  /
--  ');

END LOOP;

END;
/

