Oracle触发器 - 错误:ORA-00903:表名无效

时间:2017-01-25 10:58:30

标签: oracle plsql

我的sql代码有问题。

CREATE OR REPLACE TRIGGER
BEFORE UPDATE ON TABAPPOGGIO
FOR EACH ROW
DECLARE
ERRORE EXCEPTION;
begin
IF :NEW_ConteggioPrenotazioni>150
THEN RAISE ERRORE;
END IF;
exception
when errore then raise_application_error (-20002, 'Sala Piena');
end;

之前我为tabappoggio创建了一个查询:

CREATE VIEW TABAPPOGGIO
AS
SELECT COUNT(Codice) AS ConteggioPrenotazioni
FROM PRENOTAZIONI
WHERE Sala='101'
GROUP BY Codice

但它一直告诉我ORA-00903:表名无效。 有任何帮助,请或建议?谢谢!

编辑:抱歉!!!我不小心删除了一个人的评论,他告诉我我忘记了触发器名称。我看到了勾号并点击了它但是......我失败了。对不起,我仍然需要了解按钮是如何工作的,我在这里很新。 无论如何,谢谢!我真的忘记了触发器名称是傻瓜。我没有意识到,一切都在恐慌中。非常感谢你!!!

3 个答案:

答案 0 :(得分:2)

如果要在视图上创建触发器,则需要INSTEAD OF触发器。 INSTEAD OF触发器对视图上的DML事件有效。它们对DDL或数据库事件无效。

演示

CREATE TABLE BaseTable
  (ID     int PRIMARY KEY IDENTITY(1,1),
   Color          nvarchar(10) NOT NULL,
   Material       nvarchar(10) NOT NULL,
   ComputedCol AS (Color + Material)
  );


--Create a view that contains all columns from the base table.
CREATE VIEW InsteadView
AS SELECT ID, Color, Material, ComputedCol
FROM BaseTable;


--Create an INSTEAD OF INSERT trigger on the view.
CREATE TRIGGER InsteadTrigger on InsteadView
INSTEAD OF INSERT
AS
BEGIN

  INSERT INTO BaseTable
       SELECT Color, Material
       FROM inserted
END;

在你的情况下,它变为:

CREATE OR REPLACE TRIGGER InsteadTrigger on  TABAPPOGGIO
INSTEAD OF UPDATE OR INSERT
DECLARE
ERRORE EXCEPTION;
begin
IF :NEW.ConteggioPrenotazioni>150
THEN RAISE ERRORE;
END IF;
exception
when errore then raise_application_error (-20002, 'Sala Piena');
end;

注意:您可以同时读取:OLD和:NEW值,但不能写入:OLD或:NEW值。

答案 1 :(得分:1)

您的代码存在一些语法问题,但主要问题是,如果我了解您的需求,它将无法满足您的期望。

如果我正确地解释了您的需要,您希望防止在具有给定值sala的表中插入超过150行。 如果是这样,您可以简单地在表上构建一个触发器来检查,在插入一行或多行后执行计数。

例如:

create table PRENOTAZIONI( sala varchar2(10), codice number);

create or replace trigger checkPrenotazioni
after insert on prenotazioni
declare
    vNum   number;
    ERRORE EXCEPTION;
begin
    select count(codice)
    into vNum
    from prenotazioni
    where sala = '101';
    --
    IF vNum >150
        THEN RAISE ERRORE;
    END IF;
exception
    when errore
        then raise_application_error (-20002, 'Sala Piena');    
end; 

现在我使用sala = '101'插入150行,一切顺利:

SQL> insert into prenotazioni(sala, codice) select '101', 101 from dual connect by level <= 150;

150 rows created.

如果我尝试插入第151行,则不插入行,我有:

SQL> insert into prenotazioni(sala, codice) values ('101', 101);
insert into prenotazioni(sala, codice) values ('101', 101)
            *
ERROR at line 1:
ORA-20002: Sala Piena
ORA-06512: at "ALEK.CHECKPRENOTAZIONI", line 15
ORA-04088: error during execution of trigger 'ALEK.CHECKPRENOTAZIONI'

SQL> select count(1) from prenotazioni;

  COUNT(1)
----------
       150

SQL>

如果我尝试在一次拍摄中插入151行,那么同样如此:

SQL> truncate table prenotazioni;

Table truncated.

SQL> select count(1) from prenotazioni;

  COUNT(1)
----------
         0

SQL> insert into prenotazioni(sala, codice) select '101', 101 from dual connect by level <= 151;
insert into prenotazioni(sala, codice) select '101', 101 from dual connect by level <= 151
            *
ERROR at line 1:
ORA-20002: Sala Piena
ORA-06512: at "ALEK.CHECKPRENOTAZIONI", line 15
ORA-04088: error during execution of trigger 'ALEK.CHECKPRENOTAZIONI'

答案 2 :(得分:0)

看起来你觉得太复杂了。您希望限制表 var wasSubmitted = false; $("#msform").submit(function (e) { if(!wasSubmitted) { wasSubmitted = true; $.ajax({ url: 'https://example.com/webservice', type: 'POST', data: formData1, crossDomain: true, dataType: 'json', jsonpCallback: 'callback', success: function (data) { console.log(data); } }); return wasSubmitted; } return false; }); 中的记录,因此请为该表编写插入触发器。

PRENOTAZIONI

(也许你想要一个CREATE OR REPLACE TRIGGER trg_too_many_prenotazioni AFTER INSERT ON prenotazioni DECLARE v_count INTEGER; BEGIN select count(*) into v_count from prenotazioni where sala = 101; IF v_count > 150 THEN RAISE_APPLICATION_ERROR(-20002, 'sala piena'); END IF; END trg_too_many_prenotazioni; 表,每个SALA有一个记录,一个列允许sala,而不是硬编码150,sala = 101。)