简体   繁体   English

触发器SQL Oracle出错

[英]Error on Trigger SQL Oracle

Hi I'm having a problem while I'm creating a trigger on SQL Oracle. 嗨,我在SQL Oracle上创建触发器时遇到问题。

My trigger is: 我的触发条件是:

    create or replace trigger generatePassword
before insert on people
for each row
begin
    insert into people(nif,naame,date_birth,sex,adress,email,iban,password)
    values (:NEW.nif,:NEW.naame,:NEW.date_birth,:NEW.sex,:NEW.adress,:NEW.email,:NEW.iban,(select round(dbms_random.value(0000,9999)) from dual));
end;
/

The trigger was created successfully. 触发器已成功创建。 Then, when I try to run the command: 然后,当我尝试运行命令时:

insert into people (naame, date_birth, sex, adress, email, iban, nif, id) values ('Albert', '01-12-87', 'M', 'NY', 'albert@gmail.com', '000032134537512343231', '523456189', '70');

I get this error: 我收到此错误:

ORA-00036: maximum number of recursive SQL levels (50) exceeded ORA-06512: at "UTF8.GENERATEPASSWORD", line 2 ORA-04088: error during execution of trigger 'UTF8.GENERATEPASSWORD' ORA-06512: at "UTF8.GENERATEPASSWORD", line 2 ORA-04088: error during execution of trigger 'UTF8.GENERATEPASSWORD' ORA-06512: at "UTF8.GENERATEPASSWORD", line 2 ORA-04088: error during execution of trigger 'UTF8.GENERATEPASSWORD' ORA-06512: at "UTF8.GENERATEPASSWORD", line 2 ORA-04088: error during execution of trigger ' ORA-00036:递归SQL级别的最大数量(50)已超过ORA-06512:在“ UTF8.GENERATEPASSWORD”,第2行ORA-04088:在执行触发器“ UTF8.GENERATEPASSWORD”时发生错误ORA-06512:在“ UTF8.GENERATEPASSWORD” ”,第2行ORA-04088:执行触发器'UTF8.GENERATEPASSWORD'时发生错误ORA-06512:在“ UTF8.GENERATEPASSWORD”处,发生错误2,ORA-04088:执行触发器'UTF8.GENERATEPASSWORD'ORA-06512时发生错误“ UTF8.GENERATEPASSWORD”,第2行ORA-04088:执行触发器“期间出错

What is the problem here? 这里有什么问题?

As it has already been pointed out by Justin, you're code generates an infinite loop because the trigger fires for each insert statement included the one inside the trigger. 正如Justin所指出的那样,您的代码会生成一个无限循环,因为触发器会为包含在触发器内部的每个插入语句触发该触发器。 A possible solution is this one: 一个可能的解决方案是:

CREATE OR REPLACE TRIGGER generatePassword
BEFORE INSERT ON people
FOR EACH ROW

BEGIN
:NEW.password := round(dbms_random.value(0000,9999);

END generatePassword;
/

Whenever the trigger fires, a password is generated and added to the original insert statement. 每当触发触发器时,都会生成一个密码并将其添加到原始的insert语句中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM