简体   繁体   中英

In PL/SQL i got the following error in triggers

I'm getting the following errors

Errors for TRIGGER TRIG:

LINE/COL ERROR
-------- -----------------------------------------------------------------
11/1     PL/SQL: SQL Statement ignored
11/37    PL/SQL: ORA-00911: invalid character

Actual Question is:

Consider the following relation schemas

Emp1
empid   name    salary  dno
   Del_History
dno Rows_deleted    Date1

Write a PL/SQL block to delete records of all employees who belong to a particular department and then record the dno, no of rows deleted and date on which deletion occurred in the Del_History table.

    create or replace trigger trig after delete on emp1 for each row 

        declare

        d number:=&d;  

        begin

    if deleting then

    update Del_History set dno=d;

    update Del_History set date1=sysdate;

    update Del_History set Rows_deleted=%rowcount;

    end if;

    delete from emp1 where dno=d;

        end;

This may not be answering your question directly but some issues with the trigger as posted are:

Your trigger will execute after delete on the table for each row. There is no need to include a delete statement in your trigger. The delete has already happened.

To access column values of the deleted row use :old.column.

Since this is a row level trigger the value of sql%rowcount will always be 1.

If deleting is not necessary since the trigger is only an after delete trigger.

create or replace trigger trig
   after delete on emp1
   for each row
declare
begin

      update del_history
         set dno = :old.dno;
      update del_history
         set date1 = sysdate;
      update del_history
         set rows_deleted = sql%rowcount;  -- always 1

end;

I don't see a need for a trigger as the actual question is "Write a PL/SQL block" . Below you'll find an anonymous PL/SQL block :

A PL/SQL block is defined by the keywords DECLARE, BEGIN, EXCEPTION, and END. These keywords divide the block into a declarative part, an executable part, and an exception-handling part. Only the executable part is required.

that fullfills all the requirements.

-- Write a PL/SQL block ...
declare
  v_department_number constant number := 42;
  v_rows_deleted number;
begin
  -- ... to delete records of all employees who belong to
  -- a particular department ...
  delete from emp1 where dno = v_department_number;

  -- record number of deleted rows
  v_rows_deleted := sql%rowcount;

  -- ... and then record the dno, no of rows deleted and date on
  -- which deletion occurred in the Del_History table.
  insert into del_history (
   dno
  ,rows_deleted
  ,date1
  ) values (
   v_department_number
  ,v_rows_deleted
  ,sysdate
  );
end;
/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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