check if some attributes of new row that I will insert it exist in this table or no and if yes do something by trigger - oracle

This is my trigger, it should check if cargoPolicyNumber ,containerNumber are exist in CONTAINER_FEE table or no, if yes then will UPDATE extraFee ..
But when I run it
I faced the message “trigger is created with compilation error”
CREATE TRIGGER FEE_VIOLATION
BEFORE INSERT ON CONTAINER_FEE
FOR EACH ROW
BEGIN
IF EXISTS (SELECT cargoPolicyNumber ,containerNumber FROM CONTAINER_FEE WHERE
new.cargoPolicyNumber = cargoPolicyNumber AND new.containerNumber = containerNumber)
UPDATE CONTAINER_FEE
SET extraFee=extraFee+100
WHERE new.cargoPolicyNumber = cargoPolicyNumber AND new.containerNumber =
containerNumber;
END IF
END

You cannot access the table that is being modified in a FOR EACH ROW trigger. But you're likely modifying the row that is currently being inserted? In that case, you don't need an update statement, you can just change the value before it's inserted. Replace your update statement with:
:new.extraFee := :new.extraFee + 100;

You get error messages, because some little things are missing :
CREATE OR REPLACE TRIGGER FEE_VIOLATION
--> add "OR REPLACE" even if this is not a source of the error, you'll need this for future modifications
BEFORE INSERT ON CONTAINER_FEE
FOR EACH ROW
BEGIN
IF EXISTS (SELECT cargoPolicyNumber ,containerNumber FROM CONTAINER_FEE WHERE
new.cargoPolicyNumber = cargoPolicyNumber AND new.containerNumber = containerNumber)
THEN --> This is missing(Error)
UPDATE CONTAINER_FEE
SET extraFee=extraFee+100
WHERE new.cargoPolicyNumber = cargoPolicyNumber AND new.containerNumber =
containerNumber;
END IF; --> This semicolon is missing(Error)
END;
/ --> you need this to compile the programming unit

Related

Update after calculate for each record ORACLE

SELECT CIF_ID,
SUM (IN_VERIFIED_DEBT + IN_FAC_WITH_OTHER + IN_FAC_WITH_BANK)
from LOS_CIF_INDV
WHERE STATUS= 'ACTIVE'
GROUP By CIF_ID;
I want to update the total column again after the user manipulates the client as update, insert but it gives an error
ORA-04098: trigger 'RLOS138.UPDATE_IN_TOTAL_COMMIT' is invalid and failed re-validation
CREATE OR REPLACE TRIGGER UPDATE_IN_TOTAL_COMMIT
AFTER UPDATE ON
LOS_CIF_INDV
FOR EACH ROW
DECLARE
inactive_id number;
BEGIN
inactive_id:=
:new.IN_VERIFIED_DEBT + :new.IN_FAC_WITH_OTHER + :new.IN_FAC_WITH_BANK;
UPDAte LOS_CIF_INDV
SET IN_TOTAL_COMMIT = inactive_id
WHERE CIF_ID = :NEW.CIF_ID;
END ;
/
I have tried this again
CREATE OR REPLACE TRIGGER RLOS138.UPDATE_IN_TOTAL_COMMIT
AFTER UPDATE ON RLOS138.LOS_CIF_INDV
FOR EACH ROW
DECLARE
inactive_id number;
BEGIN
SELECT SUM (IN_VERIFIED_DEBT+IN_FAC_WITH_OTHER+IN_FAC_WITH_BANK)
into inactive_id
from LOS_CIF_INDV
WHERE STATUS= 'ACTIVE'
and CIF_ID=:NEw.CIF_ID;
update LOS_CIF_INDV
set IN_TOTAL_COMMIT = inactive_id
where CIF_ID = :NEW.CIF_ID;
END ;
/
yes [CIF_ID] is primary key
In which case this trigger has the logic you need:
CREATE OR REPLACE TRIGGER RLOS138.UPDATE_IN_TOTAL_COMMIT
BEFORE UPDATE ON RLOS138.LOS_CIF_INDV
FOR EACH ROW
BEGIN
if :new.status = 'ACTIVE'
then
:new.IN_TOTAL_COMMIT := :new.IN_VERIFIED_DEBT + :new.IN_FAC_WITH_OTHER + :new.IN_FAC_WITH_BANK;
end if;
END ;
/
I have included the check on status because you used it in your aggregation queries, even though you omitted from the first version of the trigger. I haven't included an ELSE branch, but you may wish to add one. Also, I have assumed that the three columns in the addition are guaranteed to be not null; if that's not the case you'll need to handle that.
I have put a working demo on db<>fiddle. This includes a version of the trigger which fires on inserts as well as updates, and handles null values too....
CREATE OR REPLACE TRIGGER UPDATE_IN_TOTAL_COMMIT
-- handle INSERT as well as UPDATE
BEFORE INSERT OR UPDATE ON LOS_CIF_INDV
FOR EACH ROW
BEGIN
if :new.status = 'ACTIVE'
then
-- handle any of these columns being null
:new.IN_TOTAL_COMMIT := nvl(:new.IN_VERIFIED_DEBT,0)
+ nvl(:new.IN_FAC_WITH_OTHER,0)
+ nvl(:new.IN_FAC_WITH_BANK,0);
end if;
END ;
/
Why not after you could explain it to me
Because Oracle have written triggers that way: the AFTER EACH ROW trigger uses the finalised version of the record, the state which will be written to the database. Consequently, if we want to change any values we need to use a BEFORE EACH ROW trigger. Oracle enforces this with the error you got, ORA-04084: cannot change NEW values for this trigger type.
Just a reminder: ORA-04098 is telling you there are compilation errors in your trigger code. If you're not using an IDE which tells you what these errors are you can find them with this query:
select * from all_errors
where owner = 'RLOS138'
and name = 'UPDATE_IN_TOTAL_COMMIT' ;
(Not sure if you're connecting as RLOS138 - if you are, query USER_ERRORS instead.)
If I understood correctly, You want to update all the records having CIF_ID as an updated record with the same value in the IN_TOTAL_COMMIT column.
This is not a good idea. If you have some derived column then you should use the views instead of updating its value for every insert/update using the trigger.
If you really want to update the column then you must use the combination of Row level trigger, Statement trigger, and package variables. (Search for mutating table error in the SO)
But according to me, the best solution is to use the view, something like follows:
CREATE OR REPLACE VIEW LOS_CIF_INDV_VW AS
SELECT L.*,
COALESCE(
SUM(
CASE
WHEN STATUS = 'ACTIVE' THEN
IN_VERIFIED_DEBT + IN_FAC_WITH_OTHER + IN_FAC_WITH_BANK
END
) OVER(
PARTITION BY L.CIF_ID
),
0
) AS IN_TOTAL_COMMIT
FROM LOS_CIF_INDV L;

Insert inside trigger not firing

Hi i have this simple trigger in oracle
CREATE OR REPLACE TRIGGER OCAP_CREATE_NCRB
BEFORE INSERT
ON OCAP_TBLOCAP
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
DECLARE
Defect_Type varchar2(16);
out_ varchar2(60);
BEGIN
Select A.DEFECT_TYPE into Defect_Type from OCAP_TBLDEFECT A where A.DEFECT_ID = :NEW.DEFECT;
IF Defect_Type = 'C' THEN
--Create NCRB
SP_INSERTTBLD1D2(23,LPAD(:NEW.ISSUED_BY,6,'0'),0,'0','0','035823','Draft',' ',' ',34,' ',0,461,0,0,'035105',trunc(sysdate),' ','A',Lpad(:NEW.ISSUED_BY,6,'0'),Lpad(:NEW.ISSUED_BY,6,'0'),trunc(sysdate),'A',:New.BATCH_NO,out_);
--insert action
SP_INSERTTBLFORMYACTION(Lpad(:NEW.ISSUED_BY,6,'0'), out_, Lpad(:NEW.ISSUED_BY,6,'0'), Lpad(:NEW.ISSUED_BY,'0'), 'Draft');
--Insert other affected Lots
insert into TBLD2LOT(NCRBSERIESNO,LOTNO,CREATEDBY,CREATEDDT,SEQNO) Select (out_), A.BATCH_NO,Lpad(:NEW.ISSUED_BY,6,'0'),sysdate,(TBLD2LOTSEQ.nextval) from OCAP_OTHERBATCH A where A.OCAP_ID = :NEW.OCAP_NO;
--add NCRBSeries no. to table OCAP_TBLOCAP for referencing
Update OCAP_TBLOCAP set NCRBSERIESNO = out_ where OCAP_NO = :NEW.OCAP_NO;
--Insert ocap history
END IF;
END Ocap_Create_NCRB;
/
the first 2 stored procedure is working fine but the insert query is not .
I try to excute the insert query manunaly by replacing the Out_ and the :new.Ocap_no it is working fine.
Is there something wrong in my query?
Hope someone help me out with this.
If it isn't working, then
from OCAP_OTHERBATCH A
where A.OCAP_ID = :NEW.OCAP_NO; --> this condition is never met
which means that no rows in OCAP_OTHERBATCH contain OCAP_ID value which is equal to :NEW.OCAP_NO.
Might be because of wrong letter case, CHAR datatype (right-padded with spaces up to column's full length), ... who knows. Without tables' description and sample data, it is difficult to guess.

UPDATE Column in Same Table after INSERT using trigger in Oracle

i need to update one column in the same table after inserting is done. INSERT already coded and cannot be changed . So we are going with TRIGGER approach to update one column value.
create or replace TRIGGER "BLT_TRIGGER"
AFTER INSERT ON BLT_MAPPING
FOR EACH ROW
BEGIN
UPDATE BLT_MAPPING
SET FIELD_ID = (SELECT CASE WHEN CORR_TI = 'B' THEN '12345' ELSE '54321' END
AS INSTITUTION FROM BROKER CO,LOAN LO
WHERE LO.CORR_ID = CO.CORR_ID AND LO.LOAN_ID = loanid_in_BLT_MAPPING_table
whcih_got_inserted )
END;
I am getting purge error . What is the mistake i am doing in thins trigger .
Are you trying to update the same row that you just inserted? In that case, you want to make this a BEFORE INSERT trigger, and change :NEW.fieldid before it is inserted into the table. Something like this:
create or replace TRIGGER "BLT_TRIGGER"
BEFORE INSERT ON BLT_MAPPING
FOR EACH ROW
l_new_fieldid number; -- Or whatever the correct datatype is
BEGIN
SELECT CASE WHEN CORR_TI = 'B' THEN '12345' ELSE '54321' END
INTO l_new_fieldid
FROM BROKER CO,LOAN LO
WHERE LO.CORR_ID = CO.CORR_ID
AND LO.LOAN_ID = :NEW.loanid;
:NEW.fieldid := l_new_fieldid;
END;
But this is an inferior solution. Triggers obscure what you are trying to really do and are hard to debug when there is a problem. The better solution is to correct your INSERT statement with what you're trying to really do.

Oracle Trigger Conditional Rows

I'am just exploring Trigger in Oracle.
I have table like this
Note : WT_ID Id is FK of Water Summary but not have constraint(not directly connected)
I want to make trigger in Temp_tank table, if there are update in Table Temp_tank, it will sum all temp_tank volume with same WT_ID then updated it to Water_summary.Water_Use. Because of bussiness requirement, not all water_summary data will update. in this example only Home A will be affected
This is MyCode
CREATE OR REPLACE TRIGGER UPD_WaterUse
AFTER UPDATE ON Temp_tank
DECLARE
temp_wat number;
homeA_id= 1;
BEGIN
IF (WT_ID = homeA_id) THEN
SELECT SUM(ss.Volume) INTO temp_wat
from Temp_tank ss WHERE ss.Daytime = DAYTIME and ss.WT_ID =homeA_id;
-- functionUpdate(homeA_id,Daytime,temp_wat) ;
ELSE
NULL;
END IF;
END;
/
The question is, in line
IF (WT_ID = homeA_id) THEN
when i compiled, the line is ignored because WT_ID is not identifier.
is trigger cannot accept this style of code?

PL/SQL Update Trigger Updates All Rows

New to working with PL/SQL and trying to create a statement level trigger that will change the 'Reorder' value to 'Yes' when the product quantity (p_qoh) is either less than 10 or less than two times the product minimum (p_min). And if that's not the case, then to change the 'Reorder' value to 'No'. My problem is that when I perform an update for a specific product, it changes the reorder value of all rows instead of the one I'm specifying. Can't seem to figure out where I'm going wrong, think I've been staring at it too long, any help is greatly appreciated.
CREATE OR REPLACE TRIGGER TRG_AlterProd
AFTER INSERT OR UPDATE OF p_qoh, p_min ON product
DECLARE
v_p_min product.p_min%type;
v_p_qoh product.p_qoh%type;
CURSOR v_cursor IS SELECT p_min, p_qoh FROM product;
BEGIN
OPEN v_cursor;
LOOP
FETCH v_cursor INTO v_p_min, v_p_qoh;
EXIT WHEN v_cursor%NOTFOUND;
IF v_p_qoh < (v_p_min * 2) OR v_p_qoh < 10 THEN
UPDATE product SET p_reorder = 'Yes';
ELSE
UPDATE product SET p_reorder = 'No';
END IF;
END LOOP;
END;
/
The update command :
UPDATE product SET p_reorder = 'Yes';
updates all of your rows because you are not specifying a WHERE clause.
What you can do is to retrieve the product's id (product_id) using your cursor and save it so that you would use it this way:
UPDATE product SET p_reorder = 'Yes' WHERE id = product_id;
Whoaa, this is not how you do triggers.
1 - Read the Oracle Trigger Documentation
2 - (almost) Never do a commit in a trigger. That is the domain of the calling application.
3 - There is no need to select anything related to product. You already have the product record at hand with the :new and :old pseudo records. Just update the column value in :new as needed. Example below (not checked for syntax errors, etc.);
CREATE OR REPLACE TRIGGER TRG_AlterProd
BEFORE INSERT OR UPDATE OF p_qoh, p_min ON product
FOR EACH ROW
BEGIN
IF :new.p_qoh < (:new.p_min * 2) OR :new.p_qoh < 10 THEN
:new.p_reorder = 'Yes';
ELSE
:new p_reorder = 'No';
END IF;
END;
#StevieP, If you need to commit inside a trigger, you may want to consider doing it as Autonomous Transaction.
Also, sorry if my understanding of your problem statement is wrong, but your it sounded to me like a row level trigger - are you only updating the current row or are you scanning the entire table to change status on several rows? If it's on current row, #OldProgrammer's solution seems right.
And I am just curious, if you do an UPDATE statement inside the trigger on the same table, wouldn't it generate (recursive) trigger(s)? I haven't done statement triggers like this, so sorry if this is not the expected trigger behavior.
To me a statement trigger would make more sense, if the trigger was on say, sales table, when a product is sold (inserted into sales table), it will trigger the corresponding product id records to be updated (to REORDER) in Product table. That will prevent recursion danger also.

Resources