user1748624
user1748624

Reputation: 5

I am combining two tables for a trigger and am getting issues with creating it?

CREATE OR REPLACE TRIGGER check_enumber
BEFORE INSERT on emp
FOR EACH ROW
Declare result NUMBER;
dnumber NUMBER;
d_name VARCHAR(20);
Begin
check_eno(:new.ENO, result);
IF result = -1 THEN
RAISE_APPLICATION_ERROR (-20502, 'deptno already exist');
insert into emp_audit(ENO,ENAME,SAL,DNAME) values 
(:new.ENO,:new.ENAME,:new.SAL,:new.DNAME);
END IF;

IF Find_Dn(:new.DNAME) = 1 THEN
Select DNO, DNAME INTO dnumber, d_name
FROM dept
WHERE :new.DNAME = d_name;
:new.dno = dnumber;
IF d_name = 'SALES' THEN
:new.COMM := 300;
Update DEPT Set tot_sals = tot_sals + :new.SAL;
Update DEPT Set tot_emps = tot_emps + 1;  
END IF;
END IF; 
END;
/

LINE/COL ERROR


16/13 PLS-00103: Encountered the symbol "=" when expecting one of the following: := . ( @ % ; indicator The symbol ":= was inserted before "=" to continue.

19/36 PLS-00103: Encountered the symbol "_" when expecting one of the following: . ( , * @ % & - + ; / at mod remainder rem return returning where || multiset The symbol ". was inserted before "_" to continue.

----------------------------------New Code and Error Message---------------------------

SQL> CREATE OR REPLACE TRIGGER check_enumber
  2  BEFORE INSERT on emp
  3  FOR EACH ROW
  4     Declare result NUMBER;
  5     dnumber NUMBER;
  6     d_name VARCHAR(20);
  7  Begin
  8   check_eno(:new.ENO, result);
  9   IF result = -1 THEN
 10     RAISE_APPLICATION_ERROR (-20502, 'deptno already exist');
 11     insert into emp_audit(ENO,ENAME,SAL,DNAME) values
 12             (:new.ENO,:new.ENAME,:new.SAL,:new.DNAME);
 13   END IF;
 14
 15   IF Find_Dname(:new.DNAME) = 1 THEN
 16     Select DNO, DNAME INTO dnumber, d_name
 17     FROM dept
 18     WHERE :new.DNAME :=d_name;
 19     :new.dno = dnumber;
 20     IF d_name = 'SALES' THEN
 21     :new.COMM := 300;
 22     Update DEPT Set tot_sals = tot_sals + :new.SAL;
 23     Update DEPT Set tot_emps = tot_emps + 1;
 24     END IF;
 25     END IF;
 26  END;
 27  /

Warning: Trigger created with compilation errors.

SQL> show errors; Errors for TRIGGER CHECK_ENUMBER:

LINE/COL ERROR


13/2 PL/SQL: SQL Statement ignored 15/20 PL/SQL: ORA-01745: invalid host/bind variable name 16/11 PLS-00103: Encountered the symbol "=" when expecting one of the following: := . ( @ % ; indicator The symbol ":= was inserted before "=" to continue.

Upvotes: 0

Views: 786

Answers (1)

Yogendra Singh
Yogendra Singh

Reputation: 34367

I think, this line :new.dno = dnumber; is causing the first issue(colon missing).

It should be changed to :new.dno := dnumber;

For second issue(Thanks Mr. Glenn), please update(remove space in tot _sals) Update DEPT Set tot_sals = tot _sals + :new.SAL; to

 Update DEPT Set tot_sals = tot_sals + :new.SAL;

There seems to be another issue at line WHERE :new.DNAME :=d_name;. Please remove the ":" from the statement as:

 WHERE :new.DNAME = d_name;

Upvotes: 0

Related Questions