I've just started working with Triggers. While trying to compile the next code fragment errors are listed in the log, like so: "Error(2,3): PL/SQL: SQL Statement ignored" and "Error(2,7): PL/SQL: ORA-00922: missing or invalid option".
I don't understand the problem. Can anybody help?
CREATE OR REPLACE TRIGGER CONT_VIG
BEFORE INSERT ON CONTRACTS
FOR EACH ROW
WHEN (OLD.CLIENTID = NEW.CLIENTID AND OLD.ENDDATE > NEW.STARTDATE)
BEGIN
SET (OLD.enddate = :NEW.startdate-1);
END;
look this this-oracle and this-stackoverflow
try this:
CREATE TRIGGER hr.salary_check
BEFORE INSERT OR UPDATE OF salary, job_id ON hr.employees
FOR EACH ROW
WHEN (new.job_id <> 'AD_VP')
pl/sql_block
You can't change OLD value...
replace
SET (OLD.enddate = :NEW.startdate-1);
with for example :
:NEW.startdate := sysdate ;
(I removed SET because I don't see the usefulness)
Related
Question: write a trigger that will update the invoice subtotal each time the line table is updated (a new row inserted, updated or deleted). Include the SQL statements used to test the insert, update and delete.
I'm finding it difficult to properly understand triggers and I don't know why. I understand the basic concept of it (at least I think I do) but I can't seem to understand how to answer my question. The following code is my attempt at answering the above question:
create or replace trigger update_subtotal
after insert or update or delete
on invoice
for each row
begin
insert into line ('inv_number', 'line_number', 'p_code', 'line_units', 'line_price')
values ('1009', '3', '12345-6t', '1', '123.45');
end;
select * from line;
After running this code I ended up with these errors:
Errors: TRIGGER UPDATE_SUBTOTAL
Line/Col: 3/1 PL/SQL: SQL Statement ignored
Line/Col: 3/19 PL/SQL: ORA-00928: missing SELECT keyword
Line/Col: 17/1 PLS-00103: Encountered the symbol "SELECT"
I'm using Oracle Live.
In short: help.
You seem to have the concept backwards. The invoice table needs to be updated when line changes -- so line needs the trigger and the change to invoice is an update. That would be something like this:
create or replace trigger trg_line_update_subtotal
after insert or update or delete
on line
for each row
begin
update invoice i
set total = coalesce(i.total, 0) +
coalesce(:new.line_Units * :new.line_price, 0) -
coalesce(:old.line_Units * :old.line_price, 0)
where i.inv_number = coalesce(:new.inv_number, :old.inv_number);
end;
Create a trigger called NewEntry, that will not allow a result to be inserted into the student exam table if it is less than zero. In your query display a suitable message if the result is less than zero and supply code to test the trigger.
This is what I have done, but I keep getting
warning : trrigger created with compilation errors.
This is my code, please help!
CREATE OR REPLACE TRIGGER NewEntry
AFTER INSERT OR UPDATE ON Assignment2.Student_Exam
FOR EACH ROW
DECLARE
Results NUMBER;
BEGIN
IF(:NEW.Results < '0')
THEN
RAISE_APPLICATION_ERROR
(-20700,'Student's result cannot be less-than ZERO..Enter valid Results':);
ENDIF;
END;
/
END IF, not ENDIF. Also as Jeremy C mentions above you need to create the trigger as BEFORE INSERT OR UPDATE
you listen the other answers but one more thing ,
you must focus on :NEW.Results this is the thing.
There are two errors: Firstly it is 'END IF' not ENDIF, secondly you have apostrophe's in your message.
The correct code is:
CREATE OR REPLACE TRIGGER NewEntry
AFTER INSERT OR UPDATE ON Assignment2.Student_Exam
FOR EACH ROW
DECLARE
Results NUMBER;
BEGIN
IF(:NEW.Results < '0')
THEN
RAISE_APPLICATION_ERROR
(-20700, 'students result cannot be less-than ZERO..Enter valid Results:');
END IF;
END;
/
As others have said you could have de-bugged this yourself by typing show errors; This would have given you the answer e.g.
SQL> show errors;
Errors for TRIGGER NEWENTRY:
LINE/COL ERROR
-------------------------------------------------------------------------
7/18 PLS-00103: Encountered the symbol "S" when expecting one of the
following:
) , * & = - + < / > at in is mod remainder not rem =>
<an exponent (**)> <> or != or ~= >= <= <> and or like like2
like4 likec between || multiset member submultiset
edit: In reply to Ersin - If it was New.Results then you would get a bind error e.g.
SQL> show errors;
Errors for TRIGGER NEWENTRY:
LINE/COL ERROR
-------- -----------------------------------------------------------------
4/4 PLS-00049: bad bind variable 'NEW.RESULTS'
SQL>
This will be a result of 'RESULTS' not being a column in Student_Exam, which I presume does exist given the rest of the statement.
I wrote the below trigger to prevent users from allocating a Class to a Session if the Class Date does not match the day of the week the Session is on.
CREATE OR REPLACE TRIGGER trig_alternative_classDate
AFTER INSERT OR UPDATE ON ALTERNATIVE
FOR EACH ROW
DECLARE
classdate CHAR;
sessionday VARCHAR;
BEGIN
SELECT to_char(to_date(class.class_date), 'Day') INTO classdate, sessions.day INTO sessionday
FROM SESSIONS, CLASS, DUAL, LOCATION, ALTERNATIVE
WHERE class.class_id = alternative.class_id
AND alternative.location_id = location.location_id
AND sessions.location_id = location.location_id;
IF sessions.day != to_char(to_date(class.class_date), 'Day')
THEN raise_application_error(-20999,'Invalid Class Date - Class Date does not match Session Day');
END IF;
END;
/
However I get an error message when I run the trigger
Warning: Trigger created with compilation errors.
SQL> show error trigger trig_alternative_classDate
Errors for TRIGGER TRIG_ALTERNATIVE_CLASSDATE:
LINE/COL ERROR
-------- -----------------------------------------------------------------
5/2 PL/SQL: SQL Statement ignored
5/80 PL/SQL: ORA-00923: FROM keyword not found where expected
Could someone please help?
Remove second INTO - only one INTO is needed:
INTO classdate, sessions.day INTO sessionday
I am running the following script -
BEGIN
select department_name
from egpl_department
where department_id in (select department_id
from egpl_casemgmt_activity);
END ;
And got the Error -
PLS-00103: Encountered the symbol "end-of-file" when
expecting one of the following:
;
In a PL/SQL block select statement should have an into clause:
DECLARE
v_department egpl_department.department_name%type;
BEGIN
select department_name
into v_department
from egpl_department
where department_id in (select department_id from egpl_casemgmt_activity);
-- Do something useful with v_department
END;
PLS-00103 always means the compiler has hurled because we have made a syntax error. It would be really neat if the message text said: You have made a syntax error, please check your code but alas it doesn't.
Anyway, in this case the error is that in PL/SQL select statements must populate a variable. This is different from the behaviour of say T-SQL. So you need to define a variable which matches the projection of your query and select INTO that variable.
Oracle's documentation is comprehensive and online. You can find the section on integrating SQL queries into PL/SQL here. I urge you to read it, to forestall your next question. Because once you have fixed the simple syntax bloomer you're going to hit TOO_MANY_ROWS (assuming you have more than one department).
In PL/SQL you cannot just select some data. Where is the result supposed to go?
Your options are:
Remove BEGIN and END and run the SELECT with SQL*plus or some other tool that can run a SQL statement and present the result somewhere.
Use SELECT department_name INTO dep_name to put the result into a PL/SQL variable (only works if your SELECT returns a single row)
Use SELECT department_name BULK COLLECT INTO dep_name_table to put the result into a PL/SQL table (works for several rows)
Or maybe you can describe what you're trying to achieve and in what environment you want to run the SQL or PL/SQL code.
To avoid the too_many_rows problem, you could use a cursor, something like this (I haven't tested this, but along these lines )
DECLARE
v_department egpl_department.department_name%type;
cursor c_dept IS
select department_name
into v_department
from egpl_department
where department_id in (select department_id from egpl_casemgmt_activity)
order by department_name;
BEGIN
OPEN c_dept;
FETCH c_dept INTO v_department;
CLOSE c_dept;
-- do something with v_department
END;
This will put the first value it finds in the table into v_department. Use the ORDER BY clause to make sure the row returned would be the one you required, assuming there was the possibility of 2 different values.
Most people would not consider the call to be the issue,
but here's an amusing bug in Oracle Sql Developer that may emulate the issue..
exec dbowner.sp1 ( p1, p2, p3); -- notes about the fields
Error report -
ORA-06550: line 1, column 362:
PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:
begin case declare end exception exit for goto if loop mod
null pragma raise return select update while with
<< close current delete fetch lock insert
open rollback savepoint set sql execute commit forall merge
pipe
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
exec dbowner.sp1 ( p1, p2, p3);
-- notes about the fields
PL/SQL procedure successfully completed.
DECLARE is only used in anonymous PL/SQL blocks and nested PL/SQL blocks.
You do not need to use the DECLARE key word before you 'introduce' a new variable in a Procedure block, unless .... the procedure is a nested PL/SQL block.
This is an example of how you would declare a variable without the 'DECLARE' Key word below.
eg.;
CREATE OR REPLACE PROCEDURE EXAMPLE( A IN NUMBER, B OUT VARCHAR2 )
IS
num1 number;
BEGIN
num1:=1;
insert into a (year) values(7);
END;
This question/answer explains it better
create procedure in oracle
I have a table for which i have written a trigger:
CREATE OR REPLACE TRIGGER ac01_control_trigg
AFTER INSERT ON ac1_control_test
FOR EACH ROW
DECLARE
BEGIN
IF :NEW.cur_pgm_name = 'LSN'
AND :NEW.nxt_pgm_name ='MD'
AND :NEW.file_status='RD' THEN
INSERT INTO ac1_control_test
(FILE_NAME, FILE_PATH,FILE_STATUS,CUR_PGM_NAME,NXT_PGM_NAME)
VALUES
(:NEW.FILE_NAME, :NEW.FILE_PATH,:NEW.FILE_STATUS,:NEW.CUR_PGM_NAME,'MD_MPS');
END IF;
END ac01_control_trigg;
when i am trying to insert into the table i am getting an error below!
ORA-04098: trigger 'CNGDB18.AC01_CONTROL_TRIGG' is invalid and failed re-validation
could anybody please help?
also when i compile the trigger in Toad,i am getting compile errors as below:
LINE/COL ERROR
-------- -----------------------------------------------------------------
3/65 PLS-00049: bad bind variable 'NEW_FILE_STATUS'
but what is the wrong with this?
and what does this error mean?
EDIT: Now that we see the message, the solution is easy :)
Use :NEW.file_status='RD' instead of:new_file_status='RD'
Your trigger object is invalid (there is a problem with the code).
Test this with:
SELECT object_name, status
FROM user_objects
WHERE object_name = 'AC1_CONTROL_TRIGG';
Should return:AC1_CONTROL_TRIGG INVALID
You can try the following in SQL*Plus to get a description of the error:
ALTER TRIGGER ac1_control_trigg COMPILE;
SHOW ERROR TRIGGER ac1_control_trigg;
Using TOAD, you can just type these two lines into an editor, select them and use Editor>Execute SQL via SQL*Plus.