I have one table that has a code and index attributes. I'm trying to create a trigger that will check the values of code. If there is a duplicate in the inserted code, the index will be triggered to increase by 1. Example: code|c_index 111 | 1 112 | 1 113 | 1 111 | 2 114 | 1 112 | 2 111 | 3 This is my code, but it doesn't work: PHP: create trigger trg_update after insert on trial for each row declare v_index; begin select max(nvl(trial.c_index, 0)) into v_index from trial; if new.code = code then set new.c_index = v_index else set new.c_index = 1 end if; end; ............................... I've tried to do a new better one, but still not working: PHP: create trigger trg_update after insert on trial for each row declare v_index number; begin if :new.code = :old.code then select max(nvl(c_index, 0)) into v_index from trial where code = :new.code; set :new.c_index = v_index + 1 else set :new.c_index = 1 end if; end; What's the problem with the codes above and what is the solution for the problem? ............................................... I tried another code: PHP: create trigger trg_update AFTER insert on trial for each ROW DECLARE v_index NUMBER := -1; -- "-1" is put in place so to be below the minimum value of the column DECLARE v_cnt NUMBER := 0; BEGIN SELECT MAX(c_index), COUNT(*) INTO :v_index, v_cnt FROM trial WHERE code = :new.code; IF v_index <> -1 AND v_cnt > 1 THEN --Only one update here, for the newly inserted row explicitly UPDATE trial SET c_index = c_index +1 WHERE code = :new.code AND c_index = v_index AND ROWNUM = 1; END IF; END; Some problems displayed: 1- Error(2,1): PLS-00103: Encountered the symbol "DECLARE" 2- Error(7,8): PLS-00049: bad bind variable 'V_INDEX' 3- Error(9,15): PLS-00049: bad bind variable 'NEW.CODE' How can I solve these errors??