23 April 2010

Auto Number di Oracle 10g


Skrip di bawah ini untuk membuat auto-number di database Oracle 10g

1. Cara yang Pertama

CREATE SEQUENCE ;

CREATE OR REPLACE TRIGGER
BEFORE INSERT ON
FOR EACH ROW
WHEN (NEW. IS NULL OR NEW. =1)
BEGIN
SELECT ‘PR-’ || .NEXTVAL INTO :NEW. FROM DUAL;
END;


2. Cara yang Kedua

CREATE SEQUENCE ;

CREATE TRIGGER
BEFORE INSERT ON FOR EACH ROW
BEGIN
IF (:new. IS NULL) THEN
SELECT .nextval INTO :new.
FROM DUAL;
END IF;
END;
/

3. Cara yang Ketiga

create sequence
start with 1
minvalue 1
increment by 1;

create trigger
before insert on
for each row
begin
select .nextval INTO :new.
from dual;
end;


Keterangan : Tanda <> atau kurung dihapus.

Tidak ada komentar:

Posting Komentar