Ошибка привязки переменной в триггерах Oracle PL / SQL - PullRequest
0 голосов
/ 11 октября 2019

mgr таблица - таблица с данными о сотрудниках, мгссн и зарплате. Он берет данные из таблицы сотрудников и таблицы отделов.

create table mgr as 
select ssn, mgrssn, salary 
from Employee E
join Department D 
  on E.Dno = D.Dno;

select * from mgr;

Вот мой триггер

create or replace trigger check_sal
 for insert or update on employee
 compound trigger

 type t_ch_tab is table of mgr%rowtype;
g_ch_tab      t_ch_tab := t_ch_tab();

 after each row is
begin
   g_ch_tab.extend;
   g_ch_tab(g_ch_tab.last).ssn  := :new.ssn;
   g_ch_tab(g_ch_tab.last).mgrssn    := :new.mgrssn;
   g_ch_tab(g_ch_tab.last).salary    := :new.salary;
 end after each row;


 after statement is
   l_sal employee.sal%type;
 begin
  for i in g_ch_tab.first .. g_ch_tab.last loop
     select e.salary
       into l_salary
       from employee e
       where e.ssn = g_ch_tab(i).mgrssn;

    if g_ch_tab(i).salary > l_salary then
        raise_application_error(-20001, 'Employee''s salary can not be higher than manager''s salary');
     end if;
   end loop;
 end after statement;
end check_sal;

Я получаю следующую ошибку.

Error(17,45): PLS-00049: bad bind variable 'NEW.MGRSSN'

1 Ответ

0 голосов
/ 12 октября 2019
create table department (
  mgrssn varchar2(100),
  dno number);

create table employee (
  ssn varchar2(100),
  salary number,
  dno number
);

create table mgr as select ssn,mgrssn,salary,e.dno from Employee E, Department D where E.Dno = D.Dno;

create or replace trigger check_sal
     for insert or update on employee
     compound trigger

     type t_ch_tab is table of mgr%rowtype;
     g_ch_tab      t_ch_tab := t_ch_tab();

     after each row is
    begin
       g_ch_tab.extend;
       g_ch_tab(g_ch_tab.last).ssn  := :new.ssn;
      -- g_ch_tab(g_ch_tab.last).mgrssn    := :new.mgrssn;
       g_ch_tab(g_ch_tab.last).salary    := :new.salary;
       g_ch_tab(g_ch_tab.last).dno    := :new.dno;
     end after each row;


     after statement is
       l_salary employee.salary%type;
     begin
      for i in g_ch_tab.first .. g_ch_tab.last loop
         select e.salary
           into l_salary
           from employee e, department d
          -- where e.ssn = g_ch_tab(i).mgrssn;
           where e.ssn = d.mgrssn
           and   e.dno = g_ch_tab(i).dno;

        if g_ch_tab(i).salary > l_salary then
            raise_application_error(-20001, 'Employee''s salary can not be higher than manager''s salary');
         end if;
       end loop;
     end after statement;
   end check_sal;
...