【问题标题】:Trying to create a trigger but get error PLS-00103尝试创建触发器但出现错误 PLS-00103
【发布时间】:2020-06-26 19:26:10
【问题描述】:
create or replace trigger newcontract
before insert on contract
declare numcon int;
for each row
begin
    select contractcount 
    into numcon from task
    where task.taskid = old.taskid;
    if numcon < 3 then
        insert into contract values(taskid, workerid, payment);
    else
        dbms_output.put_line('task is full');
    end if;
end;

给出这个神秘的错误

Error(1,5): PLS-00103: Encountered the symbol "FOR" when expecting one of the following:     begin function pragma procedure subtype type <an identifier>    <a double-quoted delimited-identifier> current cursor delete    exists prior 

如果该任务的合同计数约为 2,则不应插入插入到合同中的记录。因此,我需要检查要插入的每条记录的 contractcount 值。我使用 select 语句来获取值,但出现此错误。

【问题讨论】:

  • 前一行有一个分号。我认为代码是乱序的。
  • 哪一行?什么不正常?我没有发现任何问题

标签: oracle plsql oracle11g database-trigger


【解决方案1】:

你的问题不止一个:

  1. 声明部分(声明变量的触发器部分)位于for each row 部分之后
  2. OLD 和 NEW 值是这样“访问”的::new.column_name:old.column_name
  3. 插入前触发器中的:old 值始终为空,因为您正在插入一个新值,没有旧值,只有新值。
  4. 如果你想在某个值小于 3 时阻止插入,那么你可以这样做:

    create or replace trigger newcontract
    before insert on contract 
    for each row
    
    declare 
    
    numcon int;
    
    begin
    
        select contractcount 
        into numcon 
        from task
        where task.taskid = :new.taskid;
    
        if numcon < 3 then
            raise_application_error(-20000, 'Task is full');
        end if;
    
    end;
    /
    

Here is a small demo

有关更多信息,请添加一些更详细的描述和一些示例数据,您可以在其中向我们展示您希望能够插入什么样的数据以及为什么以及不想插入什么样的数据以及为什么。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-12
    • 1970-01-01
    • 2016-12-12
    相关资源
    最近更新 更多