Trigger inputing information about action in table - sql

I created table to store informations about time of any update, insert or delete data in one of tables.
CREATE TABLE dept_changes ( data DATE, action VARCHAR2(16) );
Now I want to create trigger inputing the data to table:
CREATE OR REPLACE TRIGGER dept_changes_trig AFTER UPDATE OR INSERT OR DELETE ON departments
DECLARE
action VARCHAR2(16);
BEGIN
IF UPDATING THEN
action:='upd';
END IF;
IF INSERTING THEN
action:='ins';
END IF;
IF DELETING THEN
action:='del';
END IF;
INSERT INTO DEPT_CHANGES (SYSDATE, action);
END;
I got 2 errors in line 12 (END IF of DELETING condition statement).
Error(12,5): PL/SQL: SQL Statement ignored
Error(12,46): PL/SQL: ORA-00926: missing VALUES keyword
I don't understand, what VALUES am I missing? What the trigger needs to work properly?

Use the values keyword
INSERT INTO DEPT_CHANGES VALUES (SYSDATE, action);
Insert Examples

Related

Before update trigger fails in oracle

I've created a trigger which should fire everey time I update the firstName of a worker in my table:
CREATE OR REPLACE TRIGGER trigger_before_update
BEFORE UPDATE ON t_workers
FOR EACH ROW
BEGIN
insert into t_logtable
values (pk_workerid , sysdate, :old.firstName)
END;
But every time I try to update a row, this is the error I got:
ora-04098 trigger is invalid and failed re-validation
What is wrong with the trigger?
And as you can see, the point would be to insert old values before update, isn't there any oracle solution, for an universial variable/column name? I mean not :old.firstName, but something, that would check any column, and gets any old value, that has been updated. It could be firstName, lastName, salary anything.
try this:
CREATE OR REPLACE TRIGGER trigger_before_update
BEFORE UPDATE ON t_workers
FOR EACH ROW
BEGIN
insert into t_logtable
values (pk_workerid , sysdate, :old.firstName);
END;
add ; after insert code

ORA-24344: success with compilation error - Trigger APEX

I've been working around this trigger and when I run the script it tells me the previous error message. I can't seem to figure out why it won't compile correctly, every pl/sql trigger tutorial seems to have the structure my trigger has. Code is the following:
create
or replace trigger new_artist before insert
on
Artist referencing new as nvartist declare counter number;
begin select
count( * ) into
counter
from
Performer
where
Stage_name = nvartist.Stage_name;
if counter > 0 then signal sqlstate '45000';
else insert
into
Artist
values(
nvartist.Stage_name,
nvartist.Name
);
insert
into
Performer
values(nvartist.Stage_name);
end if;
end;
It checks if the new artist already exists in its supertype (Performer), if it does exist it gives an error if it doesn't it inserts both into artist(Stage_name varchar2, Name varchar2) and Performer(Stage_name). Another subtype of Performer (and sibling to Artist) is Band(Stage_name), which in turn has a relationship with Artist. Why does the compiler yell at me for this trigger?
Thanks in advance
You may want to try this variant (I slightly modified names of your tables).
Creating tables with sample data:
CREATE table test_artist(
stage_name varchar2(100)
, name varchar2(100)
);
create table test_performer(
stage_name varchar2(100)
);
/*inserting test performer on which trigger will rise an error*/
insert into test_performer
select 'performer_1' from dual;
Creating trigger:
create or replace trigger new_artist
before insert
on TEST_ARTIST
referencing new as nvartist
for each row
declare
counter number;
begin
select count(*)
into counter
from test_performer
where Stage_name = :nvartist.Stage_name;
if counter > 0 then
--signal sqlstate '45000' ;
raise_application_error( -20001, 'No insertion with existing Performer');
else
/*you cant update the same table, in other case you'll get
ora-04091 table mutating error.
But nevertheless this values will be inserted by sql triggered this trigger.*/
--insert into test_artist values(:nvartist.Stage_name, :nvartist.Name);
insert into test_performer values(:nvartist.Stage_name);
end if;
end new_artist;
After that this insert will work, cause the is no 'performer_2' in 'test_performer' table:
insert into test_artist
select 'performer_2', 'name_2' from dual;
And this will fail:
insert into test_artist
select 'performer_1', 'name_1' from dual;

Oracle- Trigger Compiliation error

I am trying to create a trigger to check the month before inserting to the database. the followingg code was triedbut showing a complationng error as Warning: Trigger created with compilation errors.
this is the code
CREATE OR REPLACE TRIGGER tr_july
BEFORE INSERT
ON TBL_EVENT
BEGIN
SELECT EXTRACT(month FROM EVN_DATE) FROM TBL_EVENT;
IF EXTRACT (month from EVN_DATE) == 7 THEN
RAISE_APPLICATION_ERROR(-20110, 'NOT ALLOWED TO INSERT RECORDS DURING JULY');
END IF;
END;
/
I think you are looking for something like this:
CREATE OR REPLACE TRIGGER tr_july
BEFORE INSERT ON TBL_EVENT
BEGIN
IF EXTRACT (month from :new.EVN_DATE) = 7 THEN
RAISE_APPLICATION_ERROR(-20110, 'NOT ALLOWED TO INSERT RECORDS DURING JULY');
END IF;
END; /
Study the documentation, a section "Accessing Column Values in Row Triggers"
https://docs.oracle.com/cd/B19306_01/appdev.102/b14251/adfns_triggers.htm
to learn how to access columns of the current row within the trigger body.
In short: You need to use "correlation names" named NEW and OLD
Tip: run SET DEFINE OFF; before compiling the trigger to avoid bind variable substitution (variables prepended by a colon :).
Use it as:
CREATE OR REPLACE TRIGGER tr_july
BEFORE INSERT
ON TBL_EVENT
Declare
E_date date;
BEGIN
SELECT EVN_DATE into E_date FROM TBL_EVENT;
IF EXTRACT (month from E_date) = 7 THEN
RAISE_APPLICATION_ERROR(-20110, 'NOT ALLOWED TO INSERT RECORDS DURING JULY');
END IF;
END;
Note: Oracle if clause require = only once
There are multiple isues with this code.
First it is a before insert trigger, so the value you want to check is not yet in the table. You can't find it with a select.
Triggers are pl/sql code. Any value you select within a pl/sql procedure you have to 'select aaa INTO bbb from xxx; and bbb must be declared before your BEGIN.
In Oracle the equal comparison operator is a single = (not ==).
Within a trigger you have the special qualifiers :new and :old to reference the column values you are working on.
In update triggers only the :new qualifier is usable.
CREATE OR REPLACE TRIGGER tr_july
BEFORE INSERT
ON TBL_EVENT
BEGIN
IF EXTRACT (month from :new.EVN_DATE) = 7 THEN
RAISE_APPLICATION_ERROR(-20110, 'NOT ALLOWED TO INSERT RECORDS DURING JULY');
END IF;
END;

Oracle SQL trigger - DBMS_OUTPUT.PUT_LINE

I'm creating a trigger within my database, I came across two error that I am not able to fix, I'm pretty sure that those two are relating to my use of DBMS_OUTPUT.PUT_LINE, the rest of the statement does not cause any errors, although it had before.
Errors:
Error(5,3): PL/SQL: SQL Statement ignored
Error(5,15): PL/SQL: ORA-00903: invalid table name
Code:
CREATE TRIGGER INVOICES
BEFORE INSERT OR UPDATE ON BRUINVOICE
FOR EACH ROW
BEGIN
IF :new.BRU_DATE < :new.BRU_PAID_DATE THEN
DBMS_OUTPUT.PUT_LINE('You cannot do that');
ELSE
INSERT INTO table BRUINVOICE
values
from inserted;
END IF;
END;
Check constraints are a better choice (performance-wise) than triggers when it comes to record level validation:
ALTER TABLE bruinvoice
ADD CONSTRAINT validate_bru_date CHECK (BRU_DATE < BRU_PAID_DATE);
Inserting invalid data will raise an error message like the following:
scott#ORCL> insert into bruinvoice values ('21-DEC-14','20-DEC-14');
insert into bruinvoice values ('21-DEC-14','20-DEC-14')
*
ERROR at line 1:
ORA-02290: check constraint (SCOTT.VALIDATE_BRU_DATE) violated
I fully agree with cstotzer, a check constraint is much better in your situation at should be the preferred way of doing it. However, just for information this would be the trigger syntax:
CREATE TRIGGER INVOICES
BEFORE INSERT OR UPDATE ON BRUINVOICE
FOR EACH ROW
BEGIN
IF :new.BRU_DATE < :new.BRU_PAID_DATE THEN
RAISE_APPLICATION_ERROR(-20001, 'You cannot do that');
END IF;
END;
You don't need any ELSE, your INSERT or UPDATE will be simply executed in this case.

Instead of trigger , insert on view

I have this view
CREATE VIEW NaveTiconderoga AS
SELECT nume, tip, cate_arme, diametru_tun, deplasament, Nave.clasa, anul_lansarii
FROM Clase, Nave
WHERE Clase.clasa = Nave.Clasa AND Nave.Clasa = 'Ticonderoga';
I wish to create a trigger to allow inserting through this view.
I wrote the following code, but i'm sure that it isn't correct as far as the WHERE clause from the SELECT.
Any pointers please?
CREATE OR REPLACE TRIGGER ticonderoga
instead of insert on NaveTiconderoga
referencing new as new old as old
begin
insert into clase (clasa, tip, cate_arme, diametru_tun, deplasament)
values (:new.clasa, :new.tip, :new.cate_arme, :new.diametru_tun, :new.deplasament);
insert into nave (nume, clasa, anul_lansarii)
values (:new.nume, :new.clasa, :new.anul_lansarii);
end;
If you want to restrict the value inserted into the view (and thus the underlying tables), so you can't put in something that the view itself won't show, you can't use a check constraint; but you can test the value inside the trigger and throw an exception if you see something you don't like:
CREATE OR REPLACE TRIGGER ticonderoga
instead of insert on NaveTiconderoga
referencing new as new old as old
begin
if :new.clasa is null or :new.clasa != 'Ticonderoga' then
raise_application_error(-20001, 'Invalid clasa');
end if;
insert into clase (clasa, tip, cate_arme, diametru_tun, deplasament)
...
SQL Fiddle of what I think you're worried about. If you change the trigger in that to:
create trigger tr42
instead of insert on v42
begin
if :new.id != 1 then
raise_application_error(-20001, 'Invalid ID');
end if;
insert into t42 (id) values (:new.id);
end;
/
... then the second insert will fail. I think that is what you want to happen, anyway.