I created a trigger when the 'MEAIN_STAT_CODE_NO' column in the '002' table was updated.
However, when I update, a trigger error occurs.
Update statements
UPDATE TB_REN016_002 A
SET A.MEAIN_STAT_CODE_NO=7003
WHERE A.CID=11101000001;
Trigger statements
CREATE TRIGGER TRI_MEAIN_STAT_EVENT
AFTER
UPDATE OF MEAIN_STAT_CODE_NO ON TB_REN016_002
FOR EACH ROW
BEGIN
IF :NEW.MEAIN_STAT_CODE_NO = 7002 THEN
IF :OLD.MEAIN_STAT_CODE_NO = 7003 OR :OLD.MEAIN_STAT_CODE_NO = 7004 THEN
UPDATE TB_REN016_003 U
SET MEAIN_STAT_CODE_NO = 3001
WHERE U.CID=:NEW.CID
AND U.MEAIN_EVENT_CODE_NO=4003;
UPDATE TB_REN016_006 E
SET EVENT_STAT_CODE_NO = 20001
WHERE U.CID=:NEW.CID
AND E.EVENT_CODE_NO=4003
AND E.OCUR_DTM=(SELECT MAX(E2.OCUR_DTM) FROM TB_REN016_006 E2 WHERE E2.CID=:NEW.CID AND E2.EVENT_CODE_NO=4003);
END IF;
END IF;
IF :NEW.MEAIN_STAT_CODE_NO = 7003 OR NEW.MEAIN_STAT_CODE_NO = 7004 THEN
UPDATE TB_REN016_003 U
SET MEAIN_STAT_CODE_NO =
(SELECT A.EVENT_GRD_CODE_NO
FROM TB_REN016_039 A
WHERE EVENT_CODE_NO=4003
)
WHERE U.CID=:NEW.CID
AND U.MEAIN_EVENT_CODE_NO=4003;
INSERT
INTO TB_REN016_006
(
MEAIN_EVENT_SERI_NO,
CID,
OCUR_DTM,
EVENT_CODE_NO,
EVENT_GRD_CODE_NO,
EVENT_CTNT,
EVENT_FIGR,
EVENT_STAT_CODE_NO,
EVENT_TRME_STAT_CODE_NO,
EVENT_OCUR_CUS
)
VALUES
(
MEAIN_EVENT_SEQ.nextval,
:NEW.CID,
SYSDATE,
4003,
(SELECT A.EVENT_GRD_CODE_NO FROM TB_REN016_039 A WHERE EVENT_CODE_NO=4003 ),
(SELECT A.EVENT_OCUR_CUS_EXPL FROM TB_REN016_039 A WHERE EVENT_CODE_NO=4003),
-1,
20002,
21001,
(SELECT A.EVENT_DSPO_MTHD_EXPL FROM TB_REN016_039 A WHERE EVENT_CODE_NO=4003 )
);
)
END IF;
END;
/
When the column 'MEAIN_STAT_CODE_NO' is changed to 7002, update the tables 'TB_REN016_003' and 'TB_REN016_006'. If the column '700_' changes to 7003 or 7004, update the table 'TB_REN016_003' and insert the table 'TB_REN016_006'.
I've been trying for over 2 hours to get the syntax right for this trigger, but it keeps giving me compilation errors, could someone please point me in the direction that I'm going wrong?
[Compilation errors: missing equal sign line 5, sql statement ignored, line 4]
film_actor has the following fields:
actor_id
film_id
film has the following fields:
film_id
rental_rate
CREATE OR REPLACE TRIGGER TRIGGER_ACTOR_STARRED
BEFORE INSERT ON film_actor FOR EACH ROW
BEGIN
IF :new.actor_id IN (SELECT *
FROM V_ACTORS_STARRED) THEN
UPDATE film
SET rental_rate := rental_rate * 1.10
WHERE :new.actor_id = film_actor.actor_id
AND film.film_id = film_actor.film_id;
END IF;
END;
/
The view I'm trying to select from is as follows:
CREATE OR REPLACE VIEW V_ACTORS_STARRED AS
SELECT actor_id
FROM actor
WHERE actor_id IN
(SELECT actor_id
FROM film_actor
WHERE film_actor.actor_id = actor.actor_id
AND film_id IN
(SELECT film.film_id
FROM film, film_category, category
WHERE category.name = 'Documentary'
AND film.film_id = film_category.film_id
AND film_category.category_id = category.category_id
AND rental_rate = (SELECT MAX(rental_rate)
FROM film, category, film_category
WHERE category.name = 'Documentary'
AND film.film_id = film_category.film_id
AND film_category.category_id = category.category_id)));
You're executing an SQL update - it should use the regular = SQL operator, not pl/SQL's := assignment operator:
UPDATE film
SET rental_rate = rental_rate * 1.10 -- Note ":=" was replaced with "="
WHERE :new.actor_id = film_actor.actor_id
AND film.film_id = film_actor.film_id;
Remove the colon in the SET part of UPDATE statement.
UPDATE film
SET rental_rate = rental_rate * 1.10
WHERE :new.actor_id = film_actor.actor_id
AND film.film_id = film_actor.film_id;
I have a trigger which is for a few fields in a table. But for some reason, if another field is changed (which is not defined in trigger) then it still fires.
CREATE OR REPLACE TRIGGER INTEGRATION_EMPLOYMENT
AFTER UPDATE OF start_day_of_employment, end_of_employment ON hr_employment_data
FOR EACH ROW
DECLARE
BEGIN
IF UPDATING THEN
MERGE INTO ad_integration intg USING dual ON (intg.user_id = :NEW.user_id AND intg.integrated = 'NO')
WHEN MATCHED THEN
UPDATE SET
intg.start_day_of_employment = decode(:NEW.start_day_of_employment, NULL, ' ', :NEW.start_day_of_employment),
intg.end_of_employment = decode(:NEW.end_of_employment, NULL, ' ', :NEW.end_of_employment),
intg.manager_status = :NEW.manager_status,
intg.pid = (SELECT pid FROM arc.user_info WHERE user_id = :NEW.user_id),
intg.network_access_start_date = (SELECT network_access_start_date FROM hr_extension_data WHERE user_id = :NEW.user_id)
WHEN NOT MATCHED THEN
INSERT (intg.user_id, intg.start_day_of_employment, intg.end_of_employment, intg.manager_status, intg.pid, intg.network_access_start_date
VALUES (:NEW.user_id, :NEW.start_day_of_employment, :NEW.end_of_employment, :NEW.manager_status, (SELECT pid FROM arc.user_info WHERE user_id = :NEW.user_id), (SELECT network_access_start_date FROM hr_extension_data WHERE user_id = :NEW.user_id));
END IF;
END HR_ADINTEGRATION_EMPLOYMENT;
Is it because of using DUAL or something am I missing?
Cheers! :-)
If you want to leave the structure as is and only process the trigger when the specifc fields change, then just do a quick compare (new code lines 7 and 8):
CREATE OR REPLACE TRIGGER INTEGRATION_EMPLOYMENT
AFTER UPDATE OF start_day_of_employment, end_of_employment ON hr_employment_data
FOR EACH ROW
DECLARE
BEGIN
IF UPDATING
AND (:NEW.start_day_of_employment <> :OLD.start_day_of_employment
OR :NEW.end_of_employment <> :OLD.end_of_employment) THEN
MERGE INTO ad_integration intg USING dual ON (intg.user_id = :NEW.user_id AND intg.integrated = 'NO')
WHEN MATCHED THEN
UPDATE SET
intg.start_day_of_employment = decode(:NEW.start_day_of_employment, NULL, ' ', :NEW.start_day_of_employment),
intg.end_of_employment = decode(:NEW.end_of_employment, NULL, ' ', :NEW.end_of_employment),
intg.manager_status = :NEW.manager_status,
intg.pid = (SELECT pid FROM arc.user_info WHERE user_id = :NEW.user_id),
intg.network_access_start_date = (SELECT network_access_start_date FROM hr_extension_data WHERE user_id = :NEW.user_id)
WHEN NOT MATCHED THEN
INSERT (intg.user_id, intg.start_day_of_employment, intg.end_of_employment, intg.manager_status, intg.pid, intg.network_access_start_date
VALUES (:NEW.user_id, :NEW.start_day_of_employment, :NEW.end_of_employment, :NEW.manager_status, (SELECT pid FROM arc.user_info WHERE user_id = :NEW.user_id), (SELECT network_access_start_date FROM hr_extension_data WHERE user_id = :NEW.user_id));
END IF;
END HR_ADINTEGRATION_EMPLOYMENT;
quite the same question as here : Copy data from one existing row to another existing row in SQL?
but in Oracle, where update ... from and update t1, t2 are not supported.
I'll repeat it here in my own words ;
I have a table T, which looks like this :
and as the arrow shows it, I want to copy everything from r where c = 1 to e where c = 2, with t matching.
I have the select statement to get what I want to copy :
select
told.t,
told.r
from
T told
inner join
T tnew
on
told.t= tnew.t
where
told.c = 1
and
tnew.c = 2
I just don't know how to put this together in an update. An Oracle update, specifically.
try this:
update T tnew
set tnew.e = (select told.r from T told where told.c = 2 and told.t = tnew.t)
where tnew.c = 1
Sounds like the time for a bulk collects! Not as pretty as AB Cade's solution but more efficient.
declare
c_data is
select t1.rowid as rid, t2.r
from my_table t1
join my_table t2
on t1.t = t2.t
where t1.c = 2
and t2.c = 1
;
type t__data is table of c_data index by binary_integer;
t_data t__data;
begin
open c_data;
loop
fetch c_data bulk collect into t_data limit 25000;
exit when t_data.count = 0;
forall i in t_data.first .. t_data.last loop
update my_table
set e = t_data(i).r
where rowid = t_data(i).rid
;
commit;
end loop;
close c_data;
end;
/
I am trying to write a function in oracle which will use cursor to insert data in one of the table. We need select query to pick the data which needs to be inserted. This is the first cursor I am writing and it turned to be too complex.
My cursor is as below:
/* Formatted on 11/5/2011 11:26:57 AM (QP5 v5.149.1003.31008) */
DECLARE
CURSOR csgetpgmecultstrecrefrs (
update_date DATE,
sequence_type VARCHAR2,
pip_number VARCHAR2,
startfrom INT,
endon INT)
IS
SELECT /*+first_rows(25) parallel (PE,20) */
pecu.component,
pecu.component_serial_no,
TO_DATE ('11/03/2011 00:00:00', 'MM/DD/YYYY HH24:MI:SS')
date_received,
TO_DATE ('11/03/2011 00:00:00', 'MM/DD/YYYY HH24:MI:SS')
date_programmed,
pecu.date_requested,
pecu.component_model_no,
pecu.product_type,
pecu.product_model_no,
pecu.product_serial_no,
pecu.factory_source,
pecu.programming_organization,
pecu.programming_site,
pecu.program_version,
pecu.ecu_serial_no,
pecu.ecu_part_no,
pecu.ecu_level,
pecu.software_assembly_id,
jdcp_pip_swa.pip_version software_assembly_id_upgrade,
TO_DATE ('11/03/2011 00:00:00', 'MM/DD/YYYY HH24:MI:SS')
software_upgrade_date,
jdcp_pip_details.released_by software_upgraded_by,
pecu.power_bump,
pecu.fuel_system_part_no,
pecu.fuel_pump_serial_no,
pecu.rack_slope,
pecu.off_set,
pecu.ecu_hours,
pecu.cal_file,
pecu.ecu_boot_block_part_no,
jdcp_pip_details.released_by user_id,
pecu.performance_option_part_no,
pecu.vehicle_option_part_no,
'PIP' sequence_type,
pecu.flex_power_part_no,
pecu.performance_option_cd,
pecu.vehicle_option_cd,
pecu.lineage,
pecu.replaced_by_esn,
pecu.replaces_esn,
pecu.esn_copied_from,
pecu.payload_seq_no,
pecu.vehicle_system_id,
pecu.user_account,
pecu.ecu_opcode_part_no,
pecu.opcode_compat_code,
pecu.marked_qa
FROM ( ( jdcp_pip_details
INNER JOIN
jdcp_pip_pin_assc
ON jdcp_pip_details.pip_number =
jdcp_pip_pin_assc.pip_number)
INNER JOIN
jdcp_pip_swa
ON jdcp_pip_details.pip_swa_id = jdcp_pip_swa.pip_swa_id)
INNER JOIN
( (SELECT pe.component,
pe.component_serial_no,
pe.display_serial_no_13,
pe.display_serial_no_17,
pe.date_requested,
pe.component_model_no,
pe.product_type,
pe.product_model_no,
pe.product_serial_no,
pe.factory_source,
pe.programming_organization,
pe.programming_site,
pe.program_version,
pe.ecu_serial_no,
pe.ecu_part_no,
pe.ecu_level,
pe.software_assembly_id,
pe.power_bump,
pe.fuel_system_part_no,
pe.fuel_pump_serial_no,
pe.rack_slope,
pe.off_set,
pe.ecu_hours,
pe.cal_file,
pe.ecu_boot_block_part_no,
pe.performance_option_part_no,
pe.vehicle_option_part_no,
pe.flex_power_part_no,
pe.performance_option_cd,
pe.vehicle_option_cd,
pe.lineage,
pe.replaced_by_esn,
pe.replaces_esn,
pe.esn_copied_from,
pe.payload_seq_no,
pe.vehicle_system_id,
pe.user_account,
pe.ecu_opcode_part_no,
pe.opcode_compat_code,
pe.marked_qa
FROM programmed_ecu_13_17_map_view pe
WHERE pe.date_received =
(SELECT /*+ INDEX_DESC(PEDR PROGRAMMED_ECU_INDEX8) */
pedr.date_received AS date_received
FROM programmed_ecu pedr
WHERE pedr.component_serial_no =
pe.component_serial_no
AND pedr.component = pe.component
AND (pedr.date_programmed) =
(SELECT /*+ INDEX_DESC(PEDP PROGRAMMED_ECU_INDEX8) */
pedp.date_programmed
FROM programmed_ecu pedp
WHERE pedp.
component_serial_no =
pedr.
component_serial_no
AND pedp.component =
pedr.component
AND ROWNUM = 1)
AND ROWNUM = 1)) pecu
INNER JOIN
software_assembly_id
ON pecu.vehicle_system_id =
software_assembly_id.vehicle_system_id)
ON (pecu.component = jdcp_pip_pin_assc.controller_short_name)
AND (jdcp_pip_swa.pip_version =
software_assembly_id.software_assembly_id)
AND (pecu.component =
software_assembly_id.controller_short_name)
WHERE (pecu.display_serial_no_13 = jdcp_pip_pin_assc.pin_number
OR pecu.display_serial_no_17 = jdcp_pip_pin_assc.pin_number)
AND pecu.software_assembly_id <> jdcp_pip_swa.pip_version
AND jdcp_pip_details.pip_number = 'TEST_FWD_ASSC'
AND jdcp_pip_pin_assc.status_cd NOT IN
('UC', 'SU', 'FA', 'RP', 'EC')
AND jdcp_pip_pin_assc.forward_associated = 'N'
AND ROWNUM BETWEEN 1 AND 25;
rc csgetpgmecultstrecrefrs%ROWTYPE;
BEGIN
OPEN csgetpgmecultstrecrefrs (update_date date,
sequence_type varchar2,
pip_number varchar2,
startfrom int,
endon int);
LOOP
FETCH csgetpgmecultstrecrefrs BULK COLLECT INTO rc;
EXIT WHEN csgetpgmecultstrecrefrs%NOTFOUND;
INSERT
INTO programmed_ecu (component,
component_serial_no,
date_received,
date_programmed,
date_requested,
component_model_no,
product_type,
product_model_no,
product_serial_no,
factory_source,
programming_organization,
programming_site,
program_version,
ecu_serial_no,
ecu_part_no,
ecu_level,
software_assembly_id,
software_assembly_id_upgrade,
software_upgrade_date,
software_upgraded_by,
power_bump,
fuel_system_part_no,
fuel_pump_serial_no,
rack_slope,
off_set,
ecu_hours,
cal_file,
ecu_boot_block_part_no,
user_id,
performance_option_part_no,
vehicle_option_part_no,
sequence_type,
flex_power_part_no,
performance_option_cd,
vehicle_option_cd,
lineage,
replaced_by_esn,
replaces_esn,
esn_copied_from,
payload_seq_no,
vehicle_system_id,
user_account,
ecu_opcode_part_no,
opcode_compat_code,
marked_qa
)
VALUES (
rc.component,
rc.component_serial_no,
rc.date_received,
rc.date_programmed,
rc.date_requested,
rc.component_model_no,
rc.product_type,
rc.product_model_no,
rc.product_serial_no,
rc.factory_source,
rc.programming_organization,
rc.programming_site,
rc.program_version,
rc.ecu_serial_no,
rc.ecu_part_no,
rc.ecu_level,
rc.software_assembly_id,
rc.software_assembly_id_upgrade,
rc.software_upgrade_date,
rc.software_upgraded_by,
rc.power_bump,
rc.fuel_system_part_no,
rc.fuel_pump_serial_no,
rc.rack_slope,
rc.off_set,
rc.ecu_hours,
rc.cal_file,
rc.ecu_boot_block_part_no,
rc.user_id,
rc.performance_option_part_no,
rc.vehicle_option_part_no,
rc.sequence_type,
rc.flex_power_part_no,
rc.performance_option_cd,
rc.vehicle_option_cd,
rc.lineage,
rc.replaced_by_esn,
rc.replaces_esn,
rc.esn_copied_from,
rc.payload_seq_no,
rc.vehicle_system_id,
rc.user_account,
rc.ecu_opcode_part_no,
rc.opcode_compat_code,
rc.marked_qa);
END LOOP;
COMMIT;
END;
Any help is appreciated. Thanks in advance !!!
Error message I am getting is:
Error at line 2
ORA-06550: line 147, column 44:
PLS-00103: Encountered the symbol "DATE" when expecting one of the following:
. ( ) , * # % & | = - + < / > at in is mod remainder not
range rem => .. <> or != or ~= >= <= <>
and or like LIKE2_ LIKE4_ LIKEC_ as between from using ||
multiset member SUBMULTISET_
ORA-06550: line 151, column 21:
PLS-00103: Encountered the symbol ")" when expecting one of the following:
, from into bulk
ORA-06550: line 254, column 4:
PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:
end not pragma final instantiable order overriding static
member constructor map
Script Terminated on line 2."
The declaration of your cursor is incorrect. A cursor is not declared using data types, you need to remove the whole part between the brackets. The data types for each column are determined by the columns returned by the SELECT statement:
CURSOR csgetpgmecultstrecrefrs
IS
SELECT .....
See the examples in the manual for details:
http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14261/sqloperations.htm#sthref1296