如何在 Oracle 触发器中使用变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16402996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 01:38:27  来源:igfitidea点击:

How to use variables in an Oracle trigger

oracleplsqltriggers

提问by Tom

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:

我正在尝试创建一个触发器来更新验证列哈希。它不一定是 HR,但对于 sql 难题,它是 hr,表是:

   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

大多数工资系统都内置了社会保险号的有效性检查。以下示例说明如何手动检查社会保险号的有效性: 社会保险号:123 456 782 去除校验位(最后一位):2 提取第 2、4、6 和 8 位数字:2 4 6 8 Double他们:4 8 12 16 将数字相加:4 + 8 + 1 + 2 + 1 + 6 = 22 将第一、第三、第五和第七数字相加:1 + 3 + 5 + 7 = 16 总计: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.

首先,我生成了 oracle sql 代码,---我将这些废话串起来,但它有效。

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.

我试图用 SQL select 创建触发器,但它给了我一个错误,我无法在正在修改的行上运行选择。//触发器编译得很好,但是当我对表运行插入时它吐出了错误。

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....

经过一些研究,在这个站点和其他站点上,我尝试使用 :new.sin 在更新之前获取变量,但没有运气。它似乎没有更新。因此,我dbms_output.put_line( ' Updated Check Value = ' || CHECK_VAL );尝试验证是否正在收集变量。我根本没有得到它的输出......

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 oldin similar examples.

使用 :new.variable 代码......它编译得很好,但提示我输入绑定变量。"new" 默认为空,但我尝试使用变量,因为我reference new as new and old as old在类似示例中看到了一些。

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!

谢谢你的帮助!

回答by Noel

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.

您不应该使用 UPDATE 语句来更新您要插入/更新的行的列。触发器执行完成后,触发它的语句将运行并执行插入/更新。
因此,您应该简单地设置第三列的值。

:new.vfy := check_val;

No need to use CASE statement here.

这里不需要使用 CASE 语句。