sql >> Database teknologi >  >> RDS >> Oracle

Oprettelse af en triggergenererende ID-kolonneværdi før indsættelse, når nye tabeller oprettes

virker ok, hvis jeg sætter skemanavnet i DDL.

SQL> connect sys/test as sysdba
Connected.
SQL> CREATE OR REPLACE TRIGGER after_create_table_trigger
  2  AFTER CREATE ON TEST.SCHEMA
  3  DECLARE
  4  TABLE_NAME VARCHAR2(100);
  5  BEGIN
  6  IF ORA_DICT_OBJ_TYPE = 'TABLE' THEN
  7  SELECT ORA_DICT_OBJ_NAME INTO TABLE_NAME FROM DUAL;
  8  EXECUTE IMMEDIATE
  9  ('CREATE OR REPLACE TRIGGER ID_TABLE_GEN
 10    BEFORE INSERT ON TEST.' || TABLE_NAME ||
 11    ' FOR EACH ROW
 12     BEGIN
 13      SELECT TEST.AE_IDSEQ.NEXTVAL
 14       INTO :new.ID
 15       FROM dual;
 16     END;');
 17  END IF;
 18  END;
 19  /

Trigger created.

SQL> connect test/test
Connected.
SQL> create table mytab(id number primary key, a varchar2(1));

Table created.

SQL> insert into mytab (a) values ('a');

1 row created.

SQL> select * From mytab;

        ID A
---------- -
         1 a


SQL> select * from v$version;

BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bi
PL/SQL Release 10.2.0.4.0 - Production
CORE    10.2.0.4.0      Production
TNS for Linux: Version 10.2.0.4.0 - Production
NLSRTL Version 10.2.0.4.0 - Production

p.s. ingen grund til at gøre

SELECT ORA_DICT_OBJ_NAME INTO TABLE_NAME FROM DUAL;

bare indsæt det i kommandoen.

CREATE OR REPLACE TRIGGER ID_TABLE_GEN
  BEFORE INSERT ON APPROOT.' || ORA_DICT_OBJ_NAME ||



  1. MySQL Cluster (NDB) vs MySQL Replication (InnoDB) til Rails 3 apps:fordele/ulemper?

  2. Foreign Key SQL:Alt hvad du behøver at vide om udenlandske nøgleoperationer

  3. Implementering og konfiguration af ODP.NET til at fungere uden installation med Entity Framework

  4. simpel Oracle select-sætningssyntaks