I am using Oracle SQL:
Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production 0
PL/SQL Release 12.1.0.2.0 - Production 0
CORE 12.1.0.2.0 Production 0
TNS for Linux: Version 12.1.0.2.0 - Production 0
NLSRTL Version 12.1.0.2.0 - Production
I need help figuring out how to create a sequence for a key field below:
I have a table with a field named MY_ID.
MY_ID needs to be automatically generated using sequence when a record is inserted.
The rule for the sequence is a combination of a string prefix, an counter increment by 1 per day and reset at midnight, and the date.
for example: on Sept 10, we inserted 2 records, then the MY_ID should be:
PREFIX_01_20170910
PREFIX_02_20170910
on Sept 11 :
PREFIX_01_20170911
PREFIX_02_20170911
PREFIX_03_20170911
on Sept 12, the whole table might look like this
PREFIX_01_20170910
PREFIX_02_20170910
PREFIX_01_20170911
PREFIX_02_20170911
PREFIX_03_20170911
PREFIX_01_20170912
so far, all i can do with the sequence is increment by 1 regardless of the date:
CREATE SEQUENCE SEQ_MY_TABLE INCREMENT BY 1 MAXVALUE 9999999999999999999999999999 MINVALUE 1 NOCACHE;
and the trigger:
create or replace TRIGGER MY_TABLE_TRG
BEFORE INSERT ON MY_TABLE
FOR EACH ROW
BEGIN
<<COLUMN_SEQUENCES>>
BEGIN
IF INSERTING AND :NEW.MY_ID IS NULL THEN
SELECT SEQ_MY_TABLE .NEXTVAL INTO :NEW.MY_ID FROM SYS.DUAL;
END IF;
END COLUMN_SEQUENCES;
END;
Thank you!