oracle sql developer missing on keyword - sql

I keep getting the "missing on keyword" message on the following query and I'm not sure why:
CREATE OR REPLACE TRIGGER TRG_LINE_PRODUCT AFTER INSERT UPDATE OR DELETE ON
TBL_CH08_LINE FOR EACH ROW
BEGIN
IF INSERTING THEN UPDATE TBL_CH08_PRODUCT P SET P_QOH = P_QOH-:NEW.LINE.UNITS
WHERE P.P_CODE = :NEW.P.P_CODE;
END IF;
END;
/

You are missing an OR, in fact:
CREATE OR REPLACE TRIGGER TRG_LINE_PRODUCT
AFTER INSERT OR UPDATE OR DELETE ON TBL_CH08_LINE
-----------------^
FOR EACH ROW
Oracle is expecting ON after the INSERT, which is why you get that particular error.

Related

How to fix SQL Trigger

How to fix it?
CREATE TRIGGER tb_mhs2_hapus AFTER DELETE
ON tb_mhs2 FOR EACH ROW
BEGIN
INSERT INTO tb_mhs2_hapus (nama, no, alamat, org_tua, timeee) VALUES (old.nama, old.no, old.alamat, old.org_tua, SYSDATE());
END;
im confused where im wrong? "You have an error in your SQL syntax;"
First of all why your trigger name is same as table name? tb_mhs2_hapus
So your code should be:
CREATE OR REPLACE TRIGGER TRG_tb_mhs2_hapus
AFTER DELETE ON tb_mhs2
FOR EACH ROW
BEGIN
INSERT INTO tb_mhs2_hapus (nama, no, alamat, org_tua, timeee)
VALUES (old.nama, old.no, old.alamat, old.org_tua, SYSDATE());
END;

Oracle Trigger error "ORA-00942: table or view does not exist"

this is my trigger :
create trigger tampone_trigger
after insert on tamponi.numerotelpaziente
for each row
begin
IF ( :new.numerotelpaziente not in (
select numtel
from users))
then
insert into spaiati values (new.numtel);
end if;
end;
The table "Tamponi" does exists, and "numerotelpaziente" is one of the columns...
Table "USERS" also exists, and "numtel" is one of its columns...
Why on earth is giving me that one error?
The trigger is supposed to look for this new cellPhone number inserted into "Tamponi" and check if this number exists in "Users", if not it has to be add to the separated table "spaiati" , where there is a column for it..
It is perfectly connected to my personal database (i'm running my JAVAfx application onto it, and it works fine, i just need to create some triggers).
Report error -
ORA-00942: table or view does not exist
00942. 00000 - "table or view does not exist"
*Cause:
*Action:
If i use "on Tamponi" instead of giving the column aswell, the error becomes this one :
Report error -
ORA-04082: NEW or OLD references not allowed in table level triggers
04082. 00000 - "NEW or OLD references not allowed in table level triggers"
*Cause: The trigger is accessing "new" or "old" values in a table trigger.
*Action: Remove any new or old references.
if i use "on tamponi" the error is now this :
2/5 PL/SQL: Statement ignored
2/40 PLS-00405: subquery not allowed in this context
Errori: controllare il log del compilatore
Since this code will be in a trigger, you will want it to be as efficient as possible since it might be run very often. The code below should do what you are hoping to achieve with minimal context switching.
CREATE TRIGGER tampone_trigger
AFTER INSERT
ON tamponi
FOR EACH ROW
BEGIN
INSERT INTO spaiati
SELECT :new.numerotelpaziente
FROM DUAL
WHERE NOT EXISTS
(SELECT 1
FROM users u
WHERE u.numtel = :new.numerotelpaziente);
END;
/
You can try this -
create trigger tampone_trigger
after insert on tamponi
for each row
declare
v_flag boolean := false;
begin
for c in (select numtel from users)
loop
if :new.numerotelpaziente = c.numtel
then
v_flag := true;
exit;
end if;
exit when no_data_found;
end loop;
insert into spaiati values (new.numtel);
end;
I don't have a DB client available currently, but this should be close to what you want:
create trigger tampone_trigger
after insert on tamponi for each row
declare
v_exists number;
begin
select count(*) into v_exists from users u where u.numtel = :new.numerotelpaziente;
if (v_exists = 0) then
insert into spaiati values(:new:numerotelpaziente);
end if;
end;

SQLite trigger error : rollback

I get the following error when I try to create this trigger in SQLite
create trigger timeslot_check1 after insert on section
for each row
when(new.time_slot_id not in(select time_slot_id
from time_slot))
begin
rollback
end;
ERROR : near "rollback": syntax error:
As shown in the documentation, the only SQL commands allowed in a trigger body are UPDATE, INSERT, DELETE, and SELECT.
To raise an error, you must use the RAISE() function from inside a query:
CREATE TRIGGER timeslot_check1
BEFORE INSERT ON section
FOR EACH ROW
WHEN NEW.time_slot_id NOT IN (SELECT time_slot_id FROM time_slot)
BEGIN
SELECT RAISE(FAIL, "invalid timeslot");
END;
Anyway, this check can be done much easier with a foreign key.

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.

Error: ORA-00969: missing ON keyword - Oracle

I am using Oracle 11g XE database and Oracle SQL developer to execute SQL statements.
I have this SQL statement which is giving me the above compiler error when executing it.
CREATE OR REPLACE
TRIGGER "STD"."TRG_STUDENT"
BEFORE INSERT,DELETE
ON STUDENT
FOR EACH ROW
BEGIN
IF INSERTING THEN
DBMS_OUTPUT.PUT_LINE('Inserting !!');
END IF;
IF DELETING THEN
DBMS_OUTPUT.PUT_LINE('Deleting !!');
END IF;
END;
I tried some variations but I used to get other errors.
I placed the ON STUDENT just before the BEFORE INSERT,DELETE line and I get this error:
Error: ORA-04071: missing BEFORE, AFTER or INSTEAD OF keyword
What am I missing here?
BEFORE INSERT OR DELETE
More about Create Trigger syntax: http://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_7004.htm
Use BEFORE INSERT OR DELETE instead of BEFORE INSERT, DELETE. Refer coding trigger for more in detail.