尝试创建触发器但出现错误PLS-00103

时间:2020-03-15 13:03:19

标签: oracle plsql oracle11g database-trigger

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语句获取值,但出现此错误。

1 个答案:

答案 0 :(得分:2)

您在这里有多个问题:

  1. 声明部分(声明变量的触发器部分)位于for each row部分之后
  2. 通过以下方式“访问”旧值和新值::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

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