Search code examples
oracle-databaseplsqltriggers

How to use variables in an Oracle trigger


I am trying to create a trigger to update a verify column hash. It doesn't have to be HR, but for the sql puzzle it's hr, the table is:

   DESC SINERS
Name      Null     Type         
--------- -------- ------------ 
LAST_NAME          VARCHAR2(20) 
SIN  NOT NULL NUMBER(9)    
VFY            NUMBER(2)    

the puzzle is tricky, and requires a bit of code.

Most payroll systems have a built in validity check for the social insurance number. The following example illustrates how to manually check the validity of a social insurance number: Social Insurance Number: 123 456 782 Remove the check digit (the last digit): 2 Extract the 2nd, 4th, 6th and 8th digits: 2 4 6 8 Double them: 4 8 12 16 Add the digits together: 4 + 8 + 1 + 2 + 1 + 6 = 22 Add the 1st, 3rd, 5th and 7th digits: 1 + 3 + 5 + 7 = 16 TOTAL: 38

I just made up this table to simulate how to grab a variable from the update or insert statement, calculate an hash... then throw the variable back into the same table.

i.e. ... user runs this :

insert into siners (last_name, sin) values (smith, 111222333);

or 

update siners set sin = 222333444 where last_name = 'smith';

I looked around the web for examples of how to do this but it is unclear.

Is there an easy way to do this? a function perhaps?

First, I generated the oracle sql code, --- I sub-stringed the crap out of it, but it works.

SELECT NVL((NEXT_MULTI - TOTAL_SUM), 0) AS CHECK_VAL
FROM (
SELECT ((
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 1,1), 0) +
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 2,1), 0) +
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 3,1), 0) +
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 4,1), 0) +
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 5,1), 0) +
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 6,1), 0) +
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 7,1), 0) +
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 8,1), 0) +
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 9,1), 0) 
      ) + (  SUBSTR(SIN, 1, 1) + SUBSTR(SIN, 3, 1) + SUBSTR(SIN, 5, 1) + SUBSTR(SIN, 7, 1)))
     "TOTAL_SUM",  
      CASE WHEN SUBSTR(((
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 1,1), 0) +
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 2,1), 0) +
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 3,1), 0) +
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 4,1), 0) +
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 5,1), 0) +
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 6,1), 0) +
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 7,1), 0) +
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 8,1), 0) +
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 9,1), 0) 
      ) + (  SUBSTR(SIN, 1, 1) + SUBSTR(SIN, 3, 1) + SUBSTR(SIN, 5, 1) + SUBSTR(SIN, 7, 1))), 2, 1) BETWEEN 1 AND 9 THEN ((10 - (SUBSTR(((
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 1,1), 0) +
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 2,1), 0) +
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 3,1), 0) +
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 4,1), 0) +
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 5,1), 0) +
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 6,1), 0) +
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 7,1), 0) +
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 8,1), 0) +
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 9,1), 0) 
      ) + (  SUBSTR(SIN, 1, 1) + SUBSTR(SIN, 3, 1) + SUBSTR(SIN, 5, 1) + SUBSTR(SIN, 7, 1))), 2, 1))) + ((
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 1,1), 0) +
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 2,1), 0) +
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 3,1), 0) +
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 4,1), 0) +
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 5,1), 0) +
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 6,1), 0) +
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 7,1), 0) +
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 8,1), 0) +
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 9,1), 0) 
      ) + (  SUBSTR(SIN, 1, 1) + SUBSTR(SIN, 3, 1) + SUBSTR(SIN, 5, 1) + SUBSTR(SIN, 7, 1)))) END AS NEXT_MULTI
FROM SINERS);

Second, I tried to make the code into a trigger, and that is where I am stuck at the moment.

I tried to created the trigger with SQL select in it, but it gave me an error, that I couldn't run a select on a row that was being modified. //The trigger compiled fine, but it spat out the error when I ran an insert against the table.

CREATE OR REPLACE TRIGGER CHK_VFY
AFTER INSERT OR UPDATE OF SIN ON SINERS
FOR EACH ROW 

DECLARE

LAST_ONE  NUMBER(1); 
TOTAL_SUM  NUMBER(2);  
NEXT_MULTI NUMBER(2);
CHECK_VAL  NUMBER(2);

BEGIN

SELECT T_SUM INTO TOTAL_SUM
FROM (
SELECT ((
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 1,1), 0) +
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 2,1), 0) +
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 3,1), 0) +
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 4,1), 0) +
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 5,1), 0) +
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 6,1), 0) +
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 7,1), 0) +
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 8,1), 0) +
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 9,1), 0) 
      ) + (  SUBSTR(SIN, 1, 1) + SUBSTR(SIN, 3, 1) + SUBSTR(SIN, 5, 1) + SUBSTR(SIN, 7, 1)))
     "T_SUM" FROM SINERS );

SELECT LAST1 INTO LAST_ONE
FROM (
SELECT  SUBSTR(((
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 1,1), 0) +
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 2,1), 0) +
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 3,1), 0) +
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 4,1), 0) +
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 5,1), 0) +
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 6,1), 0) +
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 7,1), 0) +
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 8,1), 0) +
      NVL(SUBSTR( (SUBSTR(SIN, 2, 1)*2) || (SUBSTR(SIN, 4, 1)*2) || (SUBSTR(SIN, 6, 1)*2) || (SUBSTR(SIN, 8, 1)*2), 9,1), 0) 
      ) + (  SUBSTR(SIN, 1, 1) + SUBSTR(SIN, 3, 1) + SUBSTR(SIN, 5, 1) + SUBSTR(SIN, 7, 1))), 2, 1) LAST1 FROM SINERS);

IF LAST_ONE BETWEEN 1 AND 9 THEN 
         NEXT_MULTI := (10 - LAST_ONE) + TOTAL_SUM ;
         CHECK_VAL := (NEXT_MULTI - TOTAL_SUM);
  ELSE 
    CHECK_VAL := 0;
END IF;
UPDATE SINERS SET VFY = CHECK_VAL;

END;

After a bit of research, on this site, and others, I tried using :new.sin to grab the variable before the update, but had not luck. it doesn't seem to update. So, I dropped in the dbms_output.put_line( ' Updated Check Value = ' || CHECK_VAL ); to try and verify if the variables were being gathered. I get no output from it at all....

using the :new.variable code.... It compiles fine, but prompts me for bind variables. "new" The default is null, but I tried playing with the variables as I see some reference new as new and old as old in similar examples.

CREATE OR REPLACE TRIGGER CAL_VFY
BEFORE INSERT OR UPDATE OF SIN ON SINERS
FOR EACH ROW 

DECLARE
SINNO NUMBER(9);
LAST_ONE  NUMBER(2); 
TOTAL_SUM  NUMBER(2);  
NEXT_MULTI NUMBER(2);
CHECK_VAL  NUMBER(2);

BEGIN
  SINNO := :NEW.SIN;
  dbms_output.put_line( 'side sin = ' || sinno );
  LAST_ONE := SUBSTR(((
      NVL(SUBSTR( (SUBSTR(SINNO, 2, 1)*2) || (SUBSTR(SINNO, 4, 1)*2) || (SUBSTR(SINNO, 6, 1)*2) || (SUBSTR(SINNO, 8, 1)*2), 1,1), 0) +
      NVL(SUBSTR( (SUBSTR(SINNO, 2, 1)*2) || (SUBSTR(SINNO, 4, 1)*2) || (SUBSTR(SINNO, 6, 1)*2) || (SUBSTR(SINNO, 8, 1)*2), 2,1), 0) +
      NVL(SUBSTR( (SUBSTR(SINNO, 2, 1)*2) || (SUBSTR(SINNO, 4, 1)*2) || (SUBSTR(SINNO, 6, 1)*2) || (SUBSTR(SINNO, 8, 1)*2), 3,1), 0) +
      NVL(SUBSTR( (SUBSTR(SINNO, 2, 1)*2) || (SUBSTR(SINNO, 4, 1)*2) || (SUBSTR(SINNO, 6, 1)*2) || (SUBSTR(SINNO, 8, 1)*2), 4,1), 0) +
      NVL(SUBSTR( (SUBSTR(SINNO, 2, 1)*2) || (SUBSTR(SINNO, 4, 1)*2) || (SUBSTR(SINNO, 6, 1)*2) || (SUBSTR(SINNO, 8, 1)*2), 5,1), 0) +
      NVL(SUBSTR( (SUBSTR(SINNO, 2, 1)*2) || (SUBSTR(SINNO, 4, 1)*2) || (SUBSTR(SINNO, 6, 1)*2) || (SUBSTR(SINNO, 8, 1)*2), 6,1), 0) +
      NVL(SUBSTR( (SUBSTR(SINNO, 2, 1)*2) || (SUBSTR(SINNO, 4, 1)*2) || (SUBSTR(SINNO, 6, 1)*2) || (SUBSTR(SINNO, 8, 1)*2), 7,1), 0) +
      NVL(SUBSTR( (SUBSTR(SINNO, 2, 1)*2) || (SUBSTR(SINNO, 4, 1)*2) || (SUBSTR(SINNO, 6, 1)*2) || (SUBSTR(SINNO, 8, 1)*2), 8,1), 0) +
      NVL(SUBSTR( (SUBSTR(SINNO, 2, 1)*2) || (SUBSTR(SINNO, 4, 1)*2) || (SUBSTR(SINNO, 6, 1)*2) || (SUBSTR(SINNO, 8, 1)*2), 9,1), 0) ) + 
      (  SUBSTR(SINNO, 1, 1) + SUBSTR(SINNO, 3, 1) + SUBSTR(SINNO, 5, 1) + SUBSTR(SINNO, 7, 1))), 2, 1); 

  TOTAL_SUM := ((
      NVL(SUBSTR( (SUBSTR(SINNO, 2, 1)*2) || (SUBSTR(SINNO, 4, 1)*2) || (SUBSTR(SINNO, 6, 1)*2) || (SUBSTR(SINNO, 8, 1)*2), 1,1), 0) +
      NVL(SUBSTR( (SUBSTR(SINNO, 2, 1)*2) || (SUBSTR(SINNO, 4, 1)*2) || (SUBSTR(SINNO, 6, 1)*2) || (SUBSTR(SINNO, 8, 1)*2), 2,1), 0) +
      NVL(SUBSTR( (SUBSTR(SINNO, 2, 1)*2) || (SUBSTR(SINNO, 4, 1)*2) || (SUBSTR(SINNO, 6, 1)*2) || (SUBSTR(SINNO, 8, 1)*2), 3,1), 0) +
      NVL(SUBSTR( (SUBSTR(SINNO, 2, 1)*2) || (SUBSTR(SINNO, 4, 1)*2) || (SUBSTR(SINNO, 6, 1)*2) || (SUBSTR(SINNO, 8, 1)*2), 4,1), 0) +
      NVL(SUBSTR( (SUBSTR(SINNO, 2, 1)*2) || (SUBSTR(SINNO, 4, 1)*2) || (SUBSTR(SINNO, 6, 1)*2) || (SUBSTR(SINNO, 8, 1)*2), 5,1), 0) +
      NVL(SUBSTR( (SUBSTR(SINNO, 2, 1)*2) || (SUBSTR(SINNO, 4, 1)*2) || (SUBSTR(SINNO, 6, 1)*2) || (SUBSTR(SINNO, 8, 1)*2), 6,1), 0) +
      NVL(SUBSTR( (SUBSTR(SINNO, 2, 1)*2) || (SUBSTR(SINNO, 4, 1)*2) || (SUBSTR(SINNO, 6, 1)*2) || (SUBSTR(SINNO, 8, 1)*2), 7,1), 0) +
      NVL(SUBSTR( (SUBSTR(SINNO, 2, 1)*2) || (SUBSTR(SINNO, 4, 1)*2) || (SUBSTR(SINNO, 6, 1)*2) || (SUBSTR(SINNO, 8, 1)*2), 8,1), 0) +
      NVL(SUBSTR( (SUBSTR(SINNO, 2, 1)*2) || (SUBSTR(SINNO, 4, 1)*2) || (SUBSTR(SINNO, 6, 1)*2) || (SUBSTR(SINNO, 8, 1)*2), 9,1), 0) 
      ) + (  SUBSTR(SINNO, 1, 1) + SUBSTR(SINNO, 3, 1) + SUBSTR(SINNO, 5, 1) + SUBSTR(SINNO, 7, 1))); 

  IF LAST_ONE BETWEEN 1 AND 9 THEN 
         NEXT_MULTI := (10 - LAST_ONE) + TOTAL_SUM ;
         CHECK_VAL := (NEXT_MULTI - TOTAL_SUM);
  ELSE 
    CHECK_VAL := 0;
  END IF;

  CASE 
   WHEN INSERTING THEN
           UPDATE SINERS SET VFY = CHECK_VAL WHERE SIN = :NEW.SIN;
           dbms_output.put_line( 'New Check Value = ' || CHECK_VAL );

    WHEN UPDATING THEN
          UPDATE SINERS SET VFY = CHECK_VAL WHERE SIN = :NEW.SIN;
          dbms_output.put_line( ' Updated Check Value = ' || CHECK_VAL );
  END CASE;

END;

If you have an thoughts on how to go about this, I'm all ears....

Thanks for your help!


Solution

  • You shouldn't use UPDATE statement to update the column of the row that you are inserting/updating. Once the trigger execution is completed, the statements that triggered it will run and do the insert/update.
    So, you should simply set the value for the third column.

    :new.vfy := check_val;
    

    No need to use CASE statement here.