简体   繁体   English

Oracle 触发器错误“ORA-00942:表或视图不存在”

[英]Oracle Trigger error "ORA-00942: table or view does not exist"

this is my trigger:这是我的触发器:

    create trigger tampone_trigger
after insert on tamponi.numerotelpaziente
for each row
begin
    IF ( :new.numerotelpaziente not in ( 
    select numtel 
    from users))
    then 
    insert into spaiati values (new.numtel);
    end if;
end;

The table "Tamponi" does exists, and "numerotelpaziente" is one of the columns... Table "USERS" also exists, and "numtel" is one of its columns... Why on earth is giving me that one error?表“Tamponi”确实存在,“numerotelpaziente”是列之一......表“USERS”也存在,“numtel”是它的列之一......到底为什么给我一个错误? The trigger is supposed to look for this new cellPhone number inserted into "Tamponi" and check if this number exists in "Users", if not it has to be add to the separated table "spaiati", where there is a column for it.. It is perfectly connected to my personal database (i'm running my JAVAfx application onto it, and it works fine, i just need to create some triggers).触发器应该查找插入到“Tamponi”中的新手机号码,并检查该号码是否存在于“Users”中,如果不存在,则必须将其添加到单独的表“spaiati”中,其中有一列。 . 它完美地连接到我的个人数据库(我在上面运行我的 JAVAfx 应用程序,它工作正常,我只需要创建一些触发器)。

    Report error -
ORA-00942: table or view does not exist
00942. 00000 -  "table or view does not exist"
*Cause:    
*Action:

If i use "on Tamponi" instead of giving the column aswell, the error becomes this one:如果我使用“on Tamponi”而不是同时给出该列,则错误将变为以下错误:

Report error -
ORA-04082: NEW or OLD references not allowed in table level triggers
04082. 00000 -  "NEW or OLD references not allowed in table level triggers"
*Cause:    The trigger is accessing "new" or "old" values in a table trigger.
*Action:   Remove any new or old references.

if i use "on tamponi" the error is now this:如果我使用“on tamponi”,现在的错误是:

2/5       PL/SQL: Statement ignored
2/40      PLS-00405: subquery not allowed in this context
Errori: controllare il log del compilatore

Since this code will be in a trigger, you will want it to be as efficient as possible since it might be run very often.由于此代码将在触发器中,您会希望它尽可能高效,因为它可能会经常运行。 The code below should do what you are hoping to achieve with minimal context switching.下面的代码应该以最少的上下文切换来完成您希望实现的目标。

CREATE TRIGGER tampone_trigger
    AFTER INSERT
    ON tamponi
    FOR EACH ROW
BEGIN
    INSERT INTO spaiati
        SELECT :new.numerotelpaziente
          FROM DUAL
         WHERE NOT EXISTS
                   (SELECT 1
                      FROM users u
                     WHERE u.numtel = :new.numerotelpaziente);
END;
/

I don't have a DB client available currently, but this should be close to what you want:我目前没有可用的数据库客户端,但这应该接近你想要的:

create trigger tampone_trigger
  after insert on tamponi for each row
declare
    v_exists number;
begin
    select count(*) into v_exists from users u where u.numtel = :new.numerotelpaziente;
    if (v_exists = 0) then
        insert into spaiati values(:new:numerotelpaziente);
    end if;
end;

You can try this -你可以试试这个 -

create trigger tampone_trigger
after insert on tamponi
for each row
declare
  v_flag  boolean := false;  
begin

for c in (select numtel from users)
loop
if :new.numerotelpaziente = c.numtel 
    then 
    v_flag := true;
    exit;
     end if;
  exit when no_data_found;
 end loop;
    insert into spaiati values (new.numtel);
end;

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

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