简体   繁体   English

SQL Oracle PL / SQL上的错误:语句被忽略

[英]Error on sql oracle PL/SQL: Statement ignored

This SQL code is giving me the error "Error at line 2: PL/SQL: Statement ignored ", Im working on SQL oracle application express / APEX: I tried everything I can think of and it gives me different problems everytime. 这段SQL代码给我错误“第2行错误:PL / SQL:语句被忽略”,我正在处理SQL oracle应用程序express / APEX:我尽了所有我能想到的东西,每次都会给我带来不同的问题。

CREATE or replace TRIGGER remove_artista 
   instead of delete on V_ARTISTA
REFERENCING old AS orow
FOR EACH ROW
BEGIN
if exists(select * from Utilizadores where pessoaID = orow.pessoaID) then
        delete from Pessoas where pessoaID = orow.pessoaID;
ELSE
        delete from Artistas where pessoaID = orow.pessoaID;
        delete from Pessoas where pessoaID = orow.pessoaID;
end if;
END;

The view: 风景:

create or replace view v_artista as 
 select 
pessoaID, nome_p, sexo, data_nasc, nome_art, biografica
from Pessoas natural inner join Artistas;

EDIT: fixed a litle typo on the code. 编辑:修复了代码上的小错字。

The full error I receive from your trigger is as follows: 我从您的触发器收到的完整错误如下:

LINE/COL ERROR
-------- -----------------------------------------------------------------
2/1      PL/SQL: Statement ignored
2/4      PLS-00204: function or pseudo-column 'EXISTS' may be used inside
         a SQL statement only

Essentially, the problem is that you can't say if exists(...) as you are doing. 本质上,问题在于您无法在执行操作时说出if exists(...) Oracle doesn't let you. Oracle不允许您这样做。

Instead, try selecting the number of matching rows in the Utilizadores table into a local variable, and then using that in your if statement: 相反,尝试将Utilizadores表中匹配行的数量选择为局部变量,然后在if语句中使用该数量:

CREATE or replace TRIGGER remove_artista 
   instead of delete on V_ARTISTA
REFERENCING old AS orow
FOR EACH ROW
DECLARE
  l_count       INTEGER;
BEGIN
  select count(*) 
    into l_count
    from Utilizadores
   where pessoaID = :orow.pessoaID;

  if l_count > 0 then
        delete from Pessoas where pessoaID = :orow.pessoaID;
  ELSE
        delete from Artistas where pessoaID = :orow.pessoaID;
        delete from Pessoas where pessoaID = :orow.pessoaID;
  end if;
END;

I also needed to replace orow with :orow . 我还需orow :orow替换:orow After making this change as well, your trigger compiled for me. 进行此更改后,您的触发器也会为我编译。

I don't think you can use the IF EXISTS construct to check if a row exists. 我认为您不能使用IF EXISTS构造检查行是否存在。 You can use SELECT COUNT(*) INTO <a variable> . 您可以使用SELECT COUNT(*) INTO <a variable> However, you may not need to check if a row exists. 但是,您可能不需要检查是否存在行。 The following code would probably work: 以下代码可能会起作用:

CREATE OR REPLACE TRIGGER remove_artista 
INSTEAD OF DELETE ON V_ARTISTA
FOR EACH ROW
BEGIN
  DELETE FROM PESSOAS
  WHERE PESSOAID = :OLD.PESSOAID;

  DELETE FROM Artistas
  WHERE PESSOAID = :OLD.PESSOAID
  AND NOT EXISTS (SELECT 1 FROM UTILIZADORES WHERE PESSOAID = :OLD.PESSOAID);  

END;

The row from PESSOAS would be deleted in any case. PESSOASPESSOAS将被删除。 The row from ARTISTAS would be deleted only if the PESSOAID does not exist in UTILIZADORES . 仅当PESSOAID中不存在ARTISTAS才会删除PESSOAID中的UTILIZADORES

References : 参考文献

Check if record exists on OTN forum 检查OTN论坛上是否存在记录

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

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