Search code examples
oracle-databasetriggersinsert-update

Trigger for inserting/updating columns is not working in Oracle


I have created a trigger in Oracle. What I want to this, whether a new row is inserted or any existing row is updated I want to Insert/Update particular column based on some conditions respectively.

Below is my trigger for the same, but it's giving me some error.

create or replace TRIGGER TRG_UPD_RRSOC_ZONAL_NAME
BEFORE UPDATE ON TBL_RRSOC_STORE_INFO
FOR EACH ROW
 BEGIN      
 IF NEW.SLP_ZONAL_HEAD_NAME = 'ABC' then ZONE_NAME = 'North';
 IF NEW.SLP_ZONAL_HEAD_NAME = 'XYZ 'then ZONE_NAME = 'South';
END;

What is wrong, as I am getting error as:

Error(4,48): PLS-00103: Encountered the symbol "=" when expecting one of the following: := . ( @ % ;


Solution

  • Assuming zone_name is a column in the TBL_RRSOC_STORE_INFO table, it sounds like you want something like this. Note that

    • References to the :new pseudorecord need to be prefixed with the colon :,
    • The assignment operator is := not =,
    • If you want to change the value of the zone_name column, you'd assign a value to :new.zone_name
    • An IF statement needs an END IF. You could use ELSIF branches as well rather than duplicating the `IF statements.

    So the trigger body would be something like this

    BEGIN      
     IF :NEW.SLP_ZONAL_HEAD_NAME = 'ABC' then :new.ZONE_NAME := 'North'; end if;
     IF :NEW.SLP_ZONAL_HEAD_NAME = 'XYZ' then :new.ZONE_NAME := 'South'; end if;
    END;
    

    Or more concisely, use a case statement rather than a series of IF statements

    BEGIN
      :new.zone_name := case :NEW.SLP_ZONAL_HEAD_NAME
                             when 'ABC' then 'North'
                             when 'XYZ' then 'South'
                          end;
    END;