Creating trigger in apex - oracle

I'm unable to find what is the issue with this trigger. I'm getting this issue while complicating.
Compilation failed, line 6 (17:42:44) The line numbers associated with compilation errors are relative to the first BEGIN statement. This only affects the compilation of database triggers.
PLS-00103: Encountered the symbol "IF" when expecting one of the following: ;
BEFORE insert or update on "JANC"
for each row
begin
if inserting and :new.id is null then
:new.created_by := nvl(v('APP_USER'), USER);
:new.created := localtimestamp;
end if;
end if;
if updating then
:new.updated_by := nvl(v('APP_USER'), USER);
:new.updated := localtimestamp;
end if;
END;```

Related

PLS-00103: Encountered the symbol "UPDATE"

I created a trigger, and this is the code below, for which I got the above error. I am doing this on Oracle Live SQL. I think it is a Live SQL specific error because the same code doesn't have much problems on a local database. Here is the code below:
create or replace trigger t1
after update or insert or delete
on emp_43
declare
o char(1);
begin
if inserting then
o := 'i';
elsif updating then
o := 'u';
else
o := 'd';
end if;
insert into emp_trail values(o,sysdate);
end;
please help this noob out.
this is the snapshot of the code and error on live sql
correct syntax is
create or replace trigger t1
after update or insert or delete
on emp_43
declare o char(1);
begin
if (inserting) then
o := 'i';
elsif (updating) then
o := 'u';
else
o := 'd';
end if;
insert into emp_trail values(o,sysdate);
end;

Oracle SQL Trigger for set min Value to INT Field on NULL

I am trying to get a trigger on the table PACKS that set a min Value in INT Field - PRICE when inserting a new PACK - for example '333'. for insert calling a Procedure new pack.
thats the trigger:
CREATE OR REPLACE TRIGGER pack_min_price
BEFORE
INSERT
ON PACKS
FOR EACH ROW
BEGIN
IF new.PRICE < 333 THEN
:new.PRICE := :new.PRICE + 333;
END IF;
END;
and thats the PROCEDURE:
create or replace PROCEDURE newPack(
cntr IN PACKS.COUNTRY%TYPE,
trns IN PACKS.TRANSPORT%TYPE,
htl IN PACKS.HOTEL%TYPE,
extr IN PACKS.HOTELEXTRAS%TYPE,
othextr IN PACKS.OTHEREXTRAS%TYPE,
strdt IN PACKS.STARTDATE%TYPE,
enddt IN PACKS.ENDDATE%TYPE,
prc IN PACKS.PRICE%TYPE)
IS
BEGIN
INSERT INTO PACKS
(COUNTRY,TRANSPORT,HOTEL,HOTELEXTRAS,OTHEREXTRAS,STARTDATE,ENDDATE,PRICE)
VALUES
(cntr,trns,htl,extr,othextr,strdt,enddt,prc);
COMMIT;
END;
the error i get when i try to compile the trigger -
Compilation failed, line 2 (10:12:41) The line numbers associated with compilation errors are relative to the first BEGIN statement. This only affects the compilation of database triggers.
PLS-00201: identifier 'NEW.PRICE' must be declaredCompilation failed, line 2 (10:12:41) The line numbers associated with compilation errors are relative to the first BEGIN statement. This only affects the compilation of database triggers.
PL/SQL: Statement ignored
and when i start the procedure -
ORA-04098: trigger 'PROJECT160.PACK_MIN_PRICE' is invalid and failed re-validation
without the Trigger the Procedure is working perfectly fine. Can you help? thx
You are missing the : on the new instance in the IF statement.
IF :new.PRICE < 333 THEN
:new.PRICE := :new.PRICE + 333;
END IF;

Oracle Forms - Error 103, Encountered the symbol "END"

I'm using Oracle Forms Builder 11.
My code:
declare
type myType is varray(3000) of my_table%rowtype;
myAsset myType:=myType();
i number;
n number;
exNoInvNum exception;
begin
go_block('my_block');
first_record;
i:=1;
loop
myAsset.extend();
myAsset(i).hqId:=:my_block.hqId;
myAsset(i).deptId:=:my_block.deptId;
myAsset(i).invNum:=:my_block.invNum;
exit when :system.last_record='TRUE';
i:=i+1;
next_record;
end loop;
go_block('my_block');
first_record;
loop
if (:my_block.linkedInvNum is not null) then
n:=0;
select count(*) into n
from my_table s
where s.invNum=:my_block.linkedInvNum
and s.hqId=:my_block.hqId
and (s.deptId=:my_block.deptId
or (s.deptId is null and :my_block.deptId is null));
if (n=0) then
for i in myAsset.first .. myAsset.last loop
if (myAsset(i).invNum=:my_block.linkedInvNum
and myAsset(i).hqId=:my_block.hqId
and (myAsset(i).deptId=:my_block.deptId
or (myAsset(i).deptId is null and :my_block.deptId is null))) then
n:=1;
end if;
end loop;
end if;
if (n=0) then
raise exNoInvNum;
else
commit_form;
go_block('my_table');
clear_block(no_validate); set_item_property('my_block.generate_excel',ENABLED,property_true); set_item_property('my_block.process_data',ENABLED,property_false);
end if;
end if;
exit when :system.last_record='TRUE';
next_record;
end loop;
exception
when exNoInvNum then
message('No existing inventory number!');
when others then
null;
end;
end;
I get Error 103 ad line 2, column 1: Encountered the symbol "END"
I checked my code for typo errors, missing semicolumns and similar stuff, but it looks like everything is fine.
Any ideas?
I get Error 103 ad line 2, column 1: Encountered the symbol "END"
Well, you do have an extra END keyword in your PL/SQL block.
end;
end;
The syntax for an anonymous PL/SQL block is:
DECLARE
...
BEGIN
...
EXCEPTION
...
END;
And, this:
when others then
null;
is itself a bug in your code.
A when others is almost always a BUG unless it is immediately followed by a RAISE. Remember, for errors, RAISE –> CATCH –> HANDLE. why do we need an exception handler? To catch the errors, log them(optional), and finally do something about them.
Read WHEN OTHERS THEN NULL – A bug

ORA-06550 PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:* & = - + ; < / >

What am I missing here? It's tagging line 5 column 19....that semi-colon has to stay.
CREATE OR REPLACE TRIGGER Patient_Audit_Trigger
BEFORE DELETE OR UPDATE ON Patient
FOR EACH ROW
DECLARE
Operation VARCHAR(6);
BEGIN
IF DELETING THEN
Operation := 'D';
END IF;
IF UPDATING THEN
Operation := 'U';
END IF;
INSERT INTO Patient_Audit (patNo, patName, patAddr, patDOB, changeTime, changeBy, ActionType)
VALUES (:old.patNo, :old.patName, :old.patAddr, :old.patDOB, CURRENT_TIMESTAMP WITH TIME ZONE, USER, Operation);
END;
/
I tried to compile your trigger ( first of all I created Patient and Patient_Audit tables) and the only reason why it was unable to compile is that there is
CURRENT_TIMESTAMP WITH TIME ZONE
which should be
CURRENT_TIMESTAMP
CURRENT_TIME stamp has already time zone , unlike LOCALTIMESTAMP
This compiles
CREATE OR REPLACE TRIGGER Patient_Audit_Trigger
BEFORE DELETE OR UPDATE ON Patient
FOR EACH ROW
DECLARE
Operation VARCHAR(6);
BEGIN
IF DELETING THEN
Operation := 'D';
END IF;
IF UPDATING THEN
Operation := 'U';
END IF;
INSERT INTO Patient_Audit (patNo, patName, patAddr, patDOB, changeTime, changeBy, ActionType)
VALUES (:old.patNo, :old.patName, :old.patAddr, :old.patDOB, current_timestamp, USER, Operation);
END;
/
(Off topic: Note that use of VARCHAR is not encouraged, you should use VARCHAR2)

Trigger not able to initialise variable

I have trigger for auditing, which stored the action performed on any row of EMP table.
This trigger works fine, except in some cases (which occurs very rarely, and I cannot identify exact condition) it gives me
Oracle Error: ORA-01400: cannot insert NULL into ("MY_SCHEMA"."HIST_EMP"."ACTION")
CREATE OR REPLACE TRIGGER HIST_EMP_AIUD
AFTER UPDATE OR INSERT OR DELETE
ON EMP
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
DECLARE
v_action VARCHAR2(1) := 'D';
BEGIN
IF INSERTING THEN
v_action := 'A';
ELSIF UPDATING THEN
v_action := 'U';
END IF;
IF DELETING THEN
INSERT INTO hist_emp (source_rowid, source_date, action)
VALUES (:old.rowid, SYSDATE, v_action);
ELSIF INSERTING OR UPDATING THEN
INSERT INTO hist_emp (source_rowid, source_date, action)
VALUES (:new.rowid, SYSDATE, v_action);
END IF;
EXCEPTION
WHEN OTHERS THEN
--Code to Log
-- <some exception handling should be placed here >
END;
This generally happens when I am deleting the row, but I am not sure.
Any thought on why this will be happening? The code looks ok to me...
Something weird is going on with variable v_action initialization, I guess. Try handling all 3 possibilities:
v_action:= null;
IF INSERTING THEN
v_action := 'A';
ELSIF UPDATING THEN
v_action := 'U';
ELSIF DELETING THEN
v_action := 'D';
END IF;

Resources