I am migrating a database from MySql to an Oracle database, using SQL developer as the migration interface. I am new to Oracle, and am not sure whether this warrants a rewrite in Oracle using triggers or using auto-increment in Oracle, if that exists.
After generating the migration script For autoincrement, here is the resulting MySQL trigger code:
CREATE OR REPLACE TRIGGER Trigger_name BEFORE INSERT ON Table_name
FOR EACH ROW
DECLARE
v_newVal NUMBER(12) := 0;
v_incval NUMBER(12) := 0;
BEGIN
IF INSERTING AND :new.table_PK_id IS NULL THEN
SELECT sequence_name.NEXTVAL INTO v_newVal FROM DUAL;
IF v_newVal = 1 THEN
SELECT NVL(max(table_PK_id),0) INTO v_newVal FROM Table_name;
v_newVal := v_newVal + 1;
LOOP
EXIT WHEN v_incval>=v_newVal;
SELECT sequence_name.nextval INTO v_incval FROM dual;
END LOOP;
END IF;
:new.table_PK_id := v_newVal;
END IF;
END;
...and here is the equivalent trigger generated in Oracle:
CREATE OR REPLACE TRIGGER Trigger_name
BEFORE INSERT ON Table_name
FOR EACH ROW
BEGIN
SELECT sequence_name.nextval
INTO :new.table_PK_id
FROM dual;
END;
Should I use the Oracle DDL trigger code in my application, or does Oracle offer something better?