Error when creating Elsif Trigger Before Update - oracle

I am creating trigger when Selisih column is equal to 0, then Status column become 'Partial'. If QtyInternalUse column is equal to QtyRequested, then Status column is Completed, else, it is Not Issued
CREATE OR REPLACE TRIGGER TG_STATUSINREQ
BEFORE UPDATE OR INSERT ON M_INTERNALREQUESTERLINE
FOR EACH ROW
BEGIN
IF (:new.Selisih <> 0 ) THEN
:new.Status := 'Partial';
ELSIF :new.QtyInternalUse := :new.QtyRequested THEN
:new.Status := 'Completed';
ELSE
:new.Status := 'Not Issued';
END IF;
END;
I've tried execute it and there's error that says
PLS-00103: Encountered the symbol "=" when expecting one of the
following:
. ( * # % & = - + < / > at in is mod remainder not rem then
<an exponent (**)> <> or != or ~= >= <= <> and or like like2
like4 likec between || indicator multiset member submultiset
The symbol "* was inserted before "=" to continue.
I've tried deleting : in = but still it won't work. How can I fix this?
REVISION
I have updated the logic and the := . The trigger was created and it's like this :
BEGIN
IF (:new.QtyInternalUse = 0 ) THEN
:new.Status := 'Not Issued';
ELSIF :new.QtyInternalUse = :new.QtyRequested THEN
:new.Status := 'Completed';
ELSE
:new.Status := 'Partial';
END IF;
END;

This should be pretty easy google-resolve. Still here you go.
ELSIF :new.QtyInternalUse := :new.QtyRequested THEN
You are trying to compare but using assignment operator[:=]. Use equals[=] in stead that will resolve your problem.
ELSIF :new.QtyInternalUse = :new.QtyRequested THEN

Related

Oracle trigger after insert on

I am trying to create a trigger on ComputerTable to update the insert operation in TableWithTrigger
I have SemesterID in SemesterDim table and TimeID in TimePeriodDim table. I declare the two variables at the beginning to call the other two attributes and update the TempSemesterID and TempTimeID with a value by comparing conditions.
Here is my code:
create or replace trigger UpdateTrigger
after insert on ComputerTable
referencing new as new old as old
for each row
declare
TempSemesterID SemesterDim.SemesterID%type;
TempTimeID TimePeriodDim.TimeID%type;
begin
if (to_char(LoginDate, 'MM/DD') >= '01/01')
and (to_char(LoginDate, 'MM/DD') <= '07/15') then
TempSemesterID := 'S1';
else
TempSemesterID := 'S2';
end if;
if (to_char(LoginTime, 'HH24:MI') >= '06:00')
and (to_char(LoginTime, 'HH24:MI') < '12:00') then
TempTimeID := '1';
elsif (to_char(LoginTime, 'HH24:MI') >= '12:00')
and (to_char(LoginTime, 'HH24:MI') < '18:00') then
TempTimeID := '2';
else
TempTimeID := '3';
end if;
insert into TableWithTrigger
values (:new.LoginTime, :new.LoginDate,
:new.DegreeCode, :new.StudentNo, TempSemesterID, TempTimeID);
end UpdateTrigger;
/
I got errors like :
Error(70,3): PL/SQL: Statement ignored;
Error(70,15): PLS-00201: identifier 'LOGINDATE' must be declared;
Error(77,3): PL/SQL: Statement ignored;
Error(77,15): PLS-00201: identifier 'LOGINTIME' must be declared;
I have no ideas on how to fix them, hope someone can help me with it.
When you refer to column names in your table that the trigger is on, you need to use either :old. or :new. prefixes so the code will know whether you are referencing the old value or the new value.
The :new notation applies to columns on the owning table. It looks like you are trying to populate local variables. So, just remove the namespace from the assignments. Note that you need to include :new. when you are referencing a column. Your IF statements reference table columns, so you need to add :new. to those references, like this:
if (to_char(:new.LoginDate, 'MM/DD') >= '01/01')
and (to_char(:new.LoginDate, 'MM/DD') <= '07/15') then
TempSemesterID := 'S1';
else
TempSemesterID := 'S2';
end if;
PLS-00103: Encountered the symbol "UpdateTrigger" when expecting one of the following: if
You got that error because you used ELSE IF in the switch statement, In PL/SQL the correct syntax is ELSIF. As you have it, you have started a nested IF block, and the compiler is expecting end UpdateTrigger; to be the matching end if statement.
Here is a version of your code which compiles:
create or replace trigger UpdateTrigger
after insert on ComputerTable
for each row
declare
TempSemesterID SemesterDim.SemesterID%type;
TempTimeID TimePeriodDim.TimeID%type;
begin
if (to_char(:new.LoginDate, 'MM/DD') >= '01/01')
and (to_char(:new.LoginDate, 'MM/DD') <= '07/15') then
TempSemesterID := 'S1';
else
TempSemesterID := 'S2';
end if;
if (to_char(:new.LoginTime, 'HH24:MI') >= '06:00')
and (to_char(:new.LoginTime, 'HH24:MI') < '12:00') then
TempTimeID := '1';
elsif (to_char(:new.LoginTime, 'HH24:MI') >= '12:00')
and (to_char(:new.LoginTime, 'HH24:MI') < '18:00') then
TempTimeID := '2';
else
TempTimeID := '3';
end if;
insert into TableWithTrigger
values (:new.LoginTime, :new.LoginDate,
:new.DegreeCode, :new.StudentNo, TempSemesterID, TempTimeID);
end UpdateTrigger;
/
Here is a demo on db<>fiddle

If statement inside Trigger not working SQL(Oracle)

My goal is to create a trigger that checks if the number you are trying to enter NR_RECIBO is in the table Doc_cabecalho with the attribute TIPO_DOC = 4
CREATE OR REPLACE TRIGGER ValidaRecibo
BEFORE INSERT ON Recibo
FOR EACH ROW
DECLARE val NUMBER;
BEGIN
SELECT COUNT(*) INTO val
FROM Doc_cabecalho
WHERE (TIPO_DOC = 4 AND NR_DOCUMENTO = :NEW.NR_RECIBO);
IF val = 0
THEN (-20502, ' Only from department 4 ');
END IF;
END ValidaRecibo;
Yet, this raises the following error:
PLS-00103: Encountered the symbol "," when expecting one of the following:
* & = - + < / > at in is mod remainder not rem <> or != or ~= >= <= <> and or like like2 like4 likec as between || member submultiset
IF val = 0 THEN
raise_application_error(-20502, ' Only from department 6 ');
And you should decide between DOC_TIPO and TIPO_DOC ;)

How do I use 'if' condition inside a 'if' condition?

I am trying to use 'if' condition inside a 'if' condition to create a function.
I know the syntax looks somewhat like below, but I am unable to do it in my code.
IF sales > (quota + 200) THEN
bonus := (sales - quota)/4;
ELSE
IF sales > quota THEN
bonus := 50;
ELSE
bonus := 0;
END IF;
END IF;
Below is the program I am trying to complete. Please help me with it.
Its a function to find phone bill amount wrt given number of calls
and plan Type
CREATE OR REPLACE FUNCTION BILL(NUM_OF_CALLS NUMBER, PLAN_TYPE NUMBER) RETURN NUMBER IS :
BILL_AMT NUMBER;
MIN_1 NUMBER :=150;
MIN_2 NUMBER :=1000;
BEGIN
IF PLAN_TYPE:=150 THEN
IF NUM_OF_CALLS<150 THEN
BILL_AMT:=MIN_1;
ELSIF NUM_OF_CALLS BETWEEN 151 AND 250 THEN
BILL_AMT:=MIN_1+(NUM_OF_CALLS-150);
ELSIF NUM_OF_CALLS BETWEEN 251 AND 400 THEN
BILL_AMT:=MIN_1+(100*1)+(NUM_OF_CALLS-250)*0.5;
ELSIF NUM_OF_CALLS>400 THEN
BILL_AMT:=MIN_1+(100*1)+(150*0.5)+(NUM_OF_CALLS-400)*0.3;
END IF;
ELSE PLAN_TYPE:=500 THEN
IF NUM_OF_CALLS<1000 THEN
BILL_AMT:=MIN_2;
ELSIF NUM_OF_CALLS BETWEEN 1001 AND 1500 THEN
BILL_AMT:=MIN_2+(NUM_OF_CALLS-1000)*0.50;
ELSIF NUM_OF_CALLS>1500 THEN
BILL_AMT:=MIN_2+(500*0.50)+(NUM_OF_CALLS-1500)*0.25;
END IF;
END IF;
RETURN BILL_AMT;
END;
Below are the errors
SQL> show error
Errors for FUNCTION BILL:
LINE/COL ERROR
11/14 PLS-00103: Encountered the symbol "=" when expecting one of the
following:
. ( * # % & = - + < / > at in is mod remainder not rem then
<an exponent (**)> <> or != or ~= >= <= <> and or like like2
like4 likec between || multiset member submultiset
The symbol "* was inserted before "=" to continue.
22/22 PLS-00103: Encountered the symbol "THEN" 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
31/6 PLS-00103: Encountered the symbol "IF" when expecting one of the
following:
; <an identifier> <a double-quoted delimited-identifier>
current delete exists prior <a single-quoted SQL string>
Your answer is directly in the error output.
PLSQL uses := for assignation and = for comparison. You are intermixing these operators and causing these errors.
Read the errors closely, they exactly describe the problem.
Hint: Add small amounts of logic at a time so that you know where your problems originate. Each of us learns this at some point in time :)
Something like this should work for you:
CREATE OR REPLACE
FUNCTION BILL(NUM_OF_CALLS NUMBER, PLAN_TYPE NUMBER)
RETURN NUMBER
IS
BILL_AMT NUMBER;
MIN_1 NUMBER := 150;
MIN_2 NUMBER := 1000;
BEGIN
IF PLAN_TYPE = 150
THEN
IF NUM_OF_CALLS<150
THEN
BILL_AMT:= MIN_1;
ELSIF NUM_OF_CALLS BETWEEN 151 AND 250
THEN
BILL_AMT:=MIN_1+(NUM_OF_CALLS-150);
ELSIF NUM_OF_CALLS BETWEEN 251 AND 400
THEN
BILL_AMT:=MIN_1+(100*1)+(NUM_OF_CALLS-250)*0.5;
ELSIF NUM_OF_CALLS>400
THEN
BILL_AMT:=MIN_1+(100*1)+(150*0.5)+(NUM_OF_CALLS-400)*0.3;
END IF;
ELSIF PLAN_TYPE = 500
THEN
IF NUM_OF_CALLS<1000
THEN
BILL_AMT:=MIN_2;
ELSIF NUM_OF_CALLS BETWEEN 1001 AND 1500 THEN
BILL_AMT:=MIN_2+(NUM_OF_CALLS-1000)*0.50;
ELSIF NUM_OF_CALLS>1500 THEN
BILL_AMT:=MIN_2+(500*0.50)+(NUM_OF_CALLS-1500)*0.25;
END IF;
END IF;
RETURN BILL_AMT;
END BILL;
Hope it helps...
You can't
ELSE PLAN_TYPE:=500 THEN
But you can
ELSIF PLAN_TYPE =500 THEN
If I've got your logic flow figured out
CREATE OR REPLACE FUNCTION BILL(NUM_OF_CALLS NUMBER, PLAN_TYPE NUMBER) RETURN NUMBER IS
BILL_AMT NUMBER;
MIN_1 NUMBER :=150;
MIN_2 NUMBER :=1000;
BEGIN
IF PLAN_TYPE = 150
THEN
IF NUM_OF_CALLS<150 THEN
BILL_AMT :=MIN_1;
ELSIF NUM_OF_CALLS BETWEEN 151 AND 250 THEN
BILL_AMT:=MIN_1+(NUM_OF_CALLS-150);
ELSIF NUM_OF_CALLS BETWEEN 251 AND 400 THEN
BILL_AMT:=MIN_1+(100*1)+(NUM_OF_CALLS-250)*0.5;
ELSIF NUM_OF_CALLS>400 THEN
BILL_AMT:=MIN_1+(100*1)+(150*0.5)+(NUM_OF_CALLS-400)*0.3;
END IF;
ELSIF PLAN_TYPE = 500 THEN
IF NUM_OF_CALLS<1000 THEN
BILL_AMT:=MIN_2;
ELSIF NUM_OF_CALLS BETWEEN 1001 AND 1500 THEN
BILL_AMT:=MIN_2+(NUM_OF_CALLS-1000)*0.50;
ELSIF NUM_OF_CALLS>1500 THEN
BILL_AMT:=MIN_2+(500*0.50)+(NUM_OF_CALLS-1500)*0.25;
END IF;
END IF;
RETURN BILL_AMT;
END;

PLS-00103: Encountered the symbol ")" when expecting

i'd encountered following error when creating the following trigger. How to resolve the error? Please..
Error at line 6: PLS-00103: Encountered the symbol ")" 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
create or replace trigger totalclaimtrig2
after insert on userinfo
for each row
begin
if (:new.sgaji>'2323.41') and (:new.power>1400) then
(
:new.jumlah_claim := :new.jarak*0.7;
:new.kelas='A';
)
elsif (:new.sgaji>'2323.41') and (:new.power between 1000 and 1400) then
(
:new.jumlah_claim := :new.jarak*0.6;
:new.kelas='B';
)
elsif (:new.sgaji>'2323.41') and (:new.power between 500 and 1000) then
(
:new.jumlah_claim := :new.jarak*0.5;
:new.kelas='C';
)
elsif (:new.sgaji>'2323.41') and (:new.power between 175 and 500) then
(
:new.jumlah_claim := :new.jarak*0.45;
:new.kelas='D';
)
elsif (:new.sgaji>'2323.41') and (:new.power<175) then
(
:new.jumlah_claim := :new.jarak*0.4;
:new.kelas='E';
)
end if;
end;​
Remove all parentheses, use := instead of = for assignments, remove the weird character after the last ;, and change after insert to before insert to change the NEW values.
create or replace trigger totalclaimtrig2
before insert on userinfo
for each row
begin
if :new.sgaji>'2323.41' and :new.power>1400 then
:new.jumlah_claim := :new.jarak*0.7;
:new.kelas:='A';
elsif :new.sgaji>'2323.41' and :new.power between 1000 and 1400 then
:new.jumlah_claim := :new.jarak*0.6;
:new.kelas:='B';
elsif :new.sgaji>'2323.41' and :new.power between 500 and 1000 then
:new.jumlah_claim := :new.jarak*0.5;
:new.kelas:='C';
elsif :new.sgaji>'2323.41' and :new.power between 175 and 500 then
:new.jumlah_claim := :new.jarak*0.45;
:new.kelas:='D';
elsif :new.sgaji>'2323.41' and :new.power<175 then
:new.jumlah_claim := :new.jarak*0.4;
:new.kelas:='E';
end if;
end;

PLS-00103 when trying to call a procedure

When attempting to compile I am getting the following errors
Error(16,8): PLS-00103: Encountered the symbol "SPROLLUPEXPENSEITEM"
when expecting one of the following: := . ( # % ; The symbol ":="
was substituted for "SPROLLUPEXPENSEITEM" to continue.
and
Error(17,15): PLS-00103: Encountered the symbol "=" when expecting one
of the following: . ( * # % & = - + < / > at in is mod remainder
not rem then <> or != or ~= >= <= <> and or like
like2 like4 likec between || multiset member submultiset
create or replace
TRIGGER tr_ExpenseItem_Rollup
AFTER DELETE OR UPDATE of ExpApprAmt
ON ExpenseItem
FOR EACH ROW
DECLARE
RollupAmt Number;
BlnResult Boolean;
BEGIN
IF DELETING THEN
RollupAmt := -1 * :Old.ExpApprAmt;
End If;
IF UPDATING Then
RollupAmt := :New.ExpApprAmt - :Old.ExpApprAmt;
End IF;
Call spRollUpExpenseItem(:New.ERNo,:New.ECNo,RollupAmt,BlnResult);
If BlnResult := TRUE
--Additional Logic Here
End IF;
END;
I'm a student and very new at this, so any help would be appreciated.
call isn't a keyword in PL/SQL and to run a stored procedure you just use its name. Remove call from before spRollUpExpenseItem:
create or replace
TRIGGER tr_ExpenseItem_Rollup
AFTER DELETE OR UPDATE of ExpApprAmt
ON ExpenseItem
FOR EACH ROW
DECLARE
RollupAmt Number;
BlnResult Boolean;
BEGIN
IF DELETING THEN
RollupAmt := -1 * :Old.ExpApprAmt;
End If;
IF UPDATING Then
RollupAmt := :New.ExpApprAmt - :Old.ExpApprAmt;
End IF;
spRollUpExpenseItem(:New.ERNo,:New.ECNo,RollupAmt,BlnResult);
If BlnResult = TRUE Then
--Additional Logic Here
End IF;
END;
I believe your problem is in the last if statement. You're missing Then and you're using := which is the assignment operator, you should use = instead.
If BlnResult = TRUE Then
or just
If BlnResult Then

Resources