I wanna create a Table with auto increment the id in oracle 11g in dbvisualizer, I know that in oracle 12c Identity is there for this but in 11g I followed the below scenario and facing issue.
CREATE TABLE sample (
id NUMBER NOT NULL,
price number
);
ALTER TABLE sample
ADD (
CONSTRAINT sample_pk PRIMARY KEY (id)
);
CREATE SEQUENCE sample_sequence START WITH 1 INCREMENT BY 1 NOCACHE NOCYCLE;
But when I execute this below trigger getting the following error.
CREATE OR REPLACE TRIGGER sample_on_insert
BEFORE INSERT ON sample
FOR EACH ROW
BEGIN
select sample_sequence.nextval
into :new.id from dual;
END;
Error:
20:16:19 SUCCESS [CREATE - 0 rows, 0.339 secs] OK. No rows were affected
CREATE OR REPLACE TRIGGER sample_on_insert
BEFORE INSERT ON sample
FOR EACH ROW
BEGIN
select sample.nextval
into :new.id from dual;
20:16:19 FAILED [END - 0 rows, 0.830 secs] [Code: 900, SQL State: 42000] ORA-00900: invalid SQL statement
[Script position: 363 - 367]
END;
20:16:20 END Execution 2 statement(s) executed, 0 row(s) affected, exec/fetch time: 1.169/0.000 secs [1 successful, 1 errors]
And When I execute only below content in trigger:
select sample_sequence.nextval
into :new.id from dual;
Error:
20:31:07 FAILED [SELECT - 0 rows, 0.956 secs] [Code: 1008, SQL State: 72000] ORA-01008: not all variables bound
[Script position: 230 - 244]
select sample.nextval into :new.id from dual;
20:31:08 END Execution 1 statement(s) executed, 0 row(s) affected, exec/fetch time: 0.956/0.000 secs [0 successful, 1 errors]
Even I did in this way too the error is same:
CREATE OR REPLACE TRIGGER sample_on_insert
BEFORE INSERT ON sample
FOR EACH ROW
BEGIN
:NEW.id := sample_sequence.NextVal;
END;