I have created a trigger which inserts City as Others for all the data inserted with City name OTHERS
But it updates as null for text other than OTHERS. So I want to add IF condition for the same. Below is my trigger.
create or replace TRIGGER TGR_UPD_OTHERS_TVIPL
BEFORE INSERT OR UPDATE ON IPCOLO_IPFEE_CALC_MST
FOR EACH ROW
BEGIN
new.CITY := case :NEW.CITY
when 'OTHERS' THEN 'Others'
end;
END;
That's because you didn't say what to do "else":
:new.city := case when :new.city = 'OTHERS' then 'Others'
else :new.city
end;
Related
I want to update the inserted value in a column from a table but only if it matches a value from a different column.
So both records are in the same table but we only want to update the value test_id if the test_lead_id corresponds with certain values.
I am trying this but with no luck....
`CREATE OR REPLACE TRIGGER test_trigger
BEFORE INSERT ON "test_list"
FOR EACH ROW BEGIN
IF :old.test_id = '1000' then
:new.test_id := '2000'
WHEN test_lead_id in ('150','151');
END IF;
END;
`
Any help is appreciated.
Many thanks
There is no old value for an insert statement. Try this:
CREATE OR REPLACE TRIGGER test_trigger
BEFORE INSERT ON "test_list"
FOR EACH ROW
WHEN (NEW.test_lead_id in ('150','151'))
BEGIN
IF :new.test_id = '1000' then
:new.test_id := '2000';
END IF;
END;
Or
CREATE OR REPLACE TRIGGER test_trigger
BEFORE INSERT ON "test_list"
FOR EACH ROW
WHEN (NEW.test_lead_id in ('150','151') and new.test_id = '1000')
BEGIN
:new.test_id := '2000';
END;
Something like this?
CREATE OR REPLACE TRIGGER test_trigger
BEFORE INSERT
ON "test_list"
FOR EACH ROW
BEGIN
IF :new.text_lead_id IN ('150', '151')
AND :old.test_id = '1000'
THEN
:new.text_id := '2000';
END IF;
END;
I am calling a function in oracle in an after update trigger. The Function is returning a value that is equated to perform a select and an insert operation.
The issue is when I am calling this function in the trigger it is getting terminated, that is it is not performing the corresponding insert operation. But the function is working fine when I execute it by itself. Also, if the trigger is run by removing the condition which is returned by the function, it is getting executed as expected.
Function:
CREATE OR REPLACE FUNCTION VERIFY_FINAL
(case_id IN number)
RETURN varchar2
IS
is_marked_final varchar2(4);
loop_count number(2);
cursor c1 is
SELECT sub_case_status from
cdm_master_sub_case
where master_id = (case_id);
BEGIN
is_marked_final := 'Y';
loop_count := 0;
FOR rec in c1
LOOP
IF (rec.sub_case_status = '1') THEN
is_marked_final := 'Y';
ELSIF (rec.sub_case_status = '2') THEN
is_marked_final := 'Y';
ELSE
loop_count := loop_count + 1;
END if;
END LOOP;
IF (loop_count > 0) THEN
is_marked_final := 'N';
END if;
RETURN is_marked_final;
END;
Trigger:
CREATE OR REPLACE TRIGGER CDM_MASTER_SUB_CASE_TRIGGER
AFTER UPDATE
on CDM_MASTER_SUB_CASE
FOR EACH ROW
DECLARE
check_var varchar2(4);
unique_id varchar2(100);
transaction_id number(10);
BEGIN
transaction_id := :new.MASTER_ID;
check_var := VERIFY_FINAL(transaction_id);
IF (check_var = 'Y') THEN
select UNIQUE_CUST_ID
INTO unique_id
from ASM355.cdm_matches
where MASTER_ID = :new.MASTER_ID
and rownum = 1;
INSERT INTO tracking_final_cases (MASTER_ID,unique_cust)
values (:new.master_id,unique_id);
END if;
END;
I would appreciate it if anyone can point me in the right direction.
1.) As tmrozek points out a return of 'N' will not do the associated insert. I might suggest having an ELSE to that IF that does something to indicate if that is what is happening.
2.) I would also point out that your SELECT INTO, if it does not find a corresponding value, would cause issues. You might want to do something to ensure that this trigger is failsafe, or have you considered what you want the code to do if that situation occurs? (Error out? Insert a null unique_id?)
3.) If you are looking at the results from a different session, bear in mind that the inserted tracking_final_cases will not be visible until you commit your changes in the session that called the trigger.
I don't know your table data but it is possible to your function to return 'N' so it wouldn't meet your trigger condition (check_var = 'Y').
If you run command like that:
update CDM_MASTER_SUB_CASE
set sub_case_status = 3;
you will probably get your problem.
Thanks guys for the time, it got resolved. I was querying a select statement in the function body over a table on which the corresponding trigger was created.
I'm trying to create a trigger that when you insert an employee in the company, assign a commission of 0.10 if it is from department 80. This is what I've tried so far:
CREATE OR REPLACE TRIGGER emp_com
BEFORE INSERT ON employees
FOR EACH ROW
WHEN (NEW.DEPARTMENT_ID= '80')
BEGIN
IF INSERTING THEN
:NEW.commission_pct := 0.10;
END IF;
END;
Your trigger should work fine in its current state; however, the "IF INSERTING" bit is redundant since your trigger only fires for inserts anyway.
CREATE OR REPLACE TRIGGER emp_com
BEFORE INSERT ON employees
FOR EACH ROW
WHEN (NEW.DEPARTMENT_ID= '80')
BEGIN
:NEW.commission_pct := 0.10;
END;
I have a statement level trigger that fires whenever INSERT UPDATE or DELETE operations are performed on a table (called customers). I want to display a message (to DBMS_OUTPUT) containing the number of rows that were inserted/updated/deleted.
I just want one message for each triggering statement, eg
'4 rows were inserted into customers table'.
How can I access the number of rows that are affected by the triggering statement from INSIDE the trigger declaration, ie XXX in the code below:
CREATE OR REPLACE TRIGGER customer_changes_trigger_2
AFTER INSERT OR UPDATE OR DELETE ON customers
DECLARE
v_operation VARCHAR(10);
v_number_rows NUMBER;
BEGIN
v_number := XXX;
IF INSERTING THEN
v_operation := 'inserted';
END IF;
IF UPDATING THEN
v_operation := 'updated';
END IF;
IF DELETING THEN
v_operation := 'deleted';
END IF;
DBMS_OUTPUT.PUT_LINE
(v_number_rows|| ' rows were ' || v_operation || ' from customers.');
END;
Can't find anything in the documentation, any help appreciated!
One way is to use a global variable to track the number of rows as there is no other way to get the row count from a statement level trigger. You would then need three triggers... one statement level to initialise the variable before the statement is run, one row level to add one to the variable for each row, one statement level to use the row count however you wish. First, set up the variable and a few procedures to help it:
create or replace package PKG_ROWCOUNT is
NUMROWS number;
procedure INIT_ROWCOUNT;
procedure ADD_ONE;
function GET_ROWCOUNT
return number;
end PKG_ROWCOUNT;
/
create or replace package body PKG_ROWCOUNT as
procedure INIT_ROWCOUNT is
begin
NUMROWS := 0;
end;
procedure ADD_ONE is
begin
NUMROWS := Nvl(NUMROWS, 0) + 1;
end;
function GET_ROWCOUNT
return number is
begin
return NUMROWS;
end;
end PKG_ROWCOUNT;
/
The first trigger to initialise the variable:
create or replace trigger CUSTOMER_CHANGES_TRIGGER_1
before insert or update or delete
on CUSTOMERS
begin
PKG_ROWCOUNT.INIT_ROWCOUNT;
end;
The second to update per row:
create or replace trigger CUSTOMER_CHANGES_TRIGGER_2
after insert or update or delete
on CUSTOMERS
for each row
begin
PKG_ROWCOUNT.ADD_ONE;
end;
/
The third to display the total:
create or replace trigger CUSTOMER_CHANGES_TRIGGER_3
after insert or update or delete
on CUSTOMERS
begin
Dbms_output.
PUT_LINE(PKG_ROWCOUNT.GET_ROWCOUNT || ' rows were affected.');
end;
I'm not 100$ sure if it's available inside AFTER trigger body, but you can try examining sql%rowcount
I am trying to create a trigger. It is supposed to update any new date entry to sysdate. So far, I have the following code. However, I get "invalid table name" and "SQL statement ignored" errors.
CREATE OR REPLACE TRIGGER new_orders
AFTER INSERT ON orders
FOR EACH ROW
BEGIN
IF INSERTING THEN
UPDATE
SET order_date := SYSDATE;
END IF;
END;
/
CREATE OR REPLACE TRIGGER new_orders
BEFORE INSERT ON orders
FOR EACH ROW
BEGIN
:NEW.order_date := SYSDATE;
END;
/