Saturday, 25 November 2017

How to Create SEQUENCE


CREATE SEQUENCE  SNO_BSB

MINVALUE 0 MAXVALUE 9999

INCREMENT BY 1

START WITH 1

CACHE 20

NOORDER

 CYCLE ;

This will create sequence with minvalue of 0 and maxvalue of  9999 and this will get increment by one every time. It will start with 1 having cache value of 20 and there is noorder and cycle means it will get repeated once reached it;s limit.

How to create auto-increment trigger

create or replace TRIGGER SNO_INC_TRG

BEFORE INSERT ON Bsb
for each row

BEGIN
if inserting then
      if :NEW."SNO" is null then
         select SNO_SEQ.nextval into :NEW."SNO" from dual;
      end if;
   end if;
END;

This trigger is setting the next value of sequence into the column when the column named SNO has null value. This trigger gets executed before the insertion on table bsb and it will triggered every time the new row gets inserted.
                                               

Oracle / PLSQL: FOR LOOP

set serveroutput on; declare a1 varchar2(100); begin select count(*) into a1 from employees; for i in 1..a1 loop dbms_output.put_line...