Trigger occur error in oracle - oracle

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'.

Related

How to modify column data-type while column is in used

I have problem while I use SELECT query. Unfortunettly, the ID is store as VARCHAR which is big mistake and right now I have problem in following function
FUNCTION GET_SUB_PROJECTS(p_currentUserId IN VARCHAR,p_projectId IN INT)
RETURN SYS_REFCURSOR IS
rc SYS_REFCURSOR;
-- getSubProject
BEGIN
OPEN rc FOR
SELECT
p.*,
a.number_ AS activityNumber,
a.description AS activityDescription
FROM
projects p
LEFT JOIN
project_users_schedule_dates pusd
ON
pusd.ProjectID = p.ProjectID AND pusd.UserID = p_currentUserId
LEFT JOIN
activities a
ON
a.id = p.activity
LEFT JOIN
responsible_persons rp
ON
rp.ProjectID = p.ProjectID AND rp.UserID = p_currentUserId
LEFT JOIN
users u
ON
u.UserID = p_currentUserId
WHERE
(u.User_roleID = 1 AND
p.CustomName LIKE CONCAT((SELECT CustomName FROM projects pr WHERE pr.ProjectID = p_projectId), '%') AND p.ProjectID <> p_projectId)
OR
((
(p.Responsible_person_id = p_currentUserId OR p.Delivery_contact = p_currentUserId OR rp.UserID = p_currentUserId OR (pusd.UserID = p_currentUserId AND SYSTIMESTAMP BETWEEN TO_DATE(pusd.StartDate,'YYYY-MM-DD') AND TO_DATE(pusd.EndDate,'YYYY-MM-DD') + INTERVAL '1' DAY AND
SYSTIMESTAMP BETWEEN TO_DATE(p.StartDate,'YYYY-MM-DD') AND TO_DATE(p.EndDate,'YYYY-MM-DD') + INTERVAL '1' DAY))
)
AND
p.CustomName LIKE CONCAT((SELECT CustomName FROM projects pr WHERE pr.ProjectID = p_projectId), '%') AND p.ProjectID <> p_projectId)
ORDER BY p.CustomName;
RETURN rc;
END GET_SUB_PROJECTS;
When I call function it needs to return data, but it doesn't. Somehow, here p.Responsible_person_id and p.Delivery_contact needs to be NUMBER but somehow it is VARCHAR
When I call function I use
SELECT PROJECT_PACKAGE.GET_SUB_PROJECTS('199',141) FROM DUAL
I found one solution to modify but It throw me error like
Error report -
ORA-01439: column to be modified must be empty to change datatype
01439. 00000 - "column to be modified must be empty to change datatype"
What to do in this situation ? What is the best method to solve this issue ?
You can change the data type of a column online using dbms_redefinition
create table t (
c1 varchar2(10)
primary key
);
create table t_new (
c1 number(10, 0)
primary key
);
insert into t values ( '1.0000' );
commit;
begin
dbms_redefinition.start_redef_table (
user, 't', 't_new',
col_mapping => 'to_number ( c1 ) c1',
options_flag => dbms_redefinition.cons_use_rowid
);
end;
/
exec dbms_redefinition.sync_interim_table ( user, 't', 't_new' );
exec dbms_redefinition.finish_redef_table ( user, 't', 't_new' );
desc t
Name Null? Type
C1 NOT NULL NUMBER(10)
select * from t;
C1
1
There are also options to copy constraints, triggers, indexes, etc. automatically in this process.

Unexpected NULL in multi-column correlated update

I want to run a multi-column correlated update of this kind:
UPDATE t1 t1_alias
SET (table_name, tablespace_name) = (
SELECT table_name, tablespace_name
FROM t2 t2_alias
WHERE t1_alias.table_name = t2_alias.table_name
);
But my attempt:
update customer up
set (customer_name, account, active) = (
select tmp.name, tmp.account, case when tmp.active = 'Yes' then 1 else 0 end
from customer_temp tmp
where up.agent = substr(tmp.agent, -4) and up.customer_code = tmp.code
);
... throws:
ORA-01407: cannot update ("FOO"."CUSTOMER"."CUSTOMER_NAME") to NULL
The source table customer_temp has no null values so I must be getting matches wrong. What is my error or misconception?
Presumably, there are some rows in the target table that have no match in the subquery.
You can avoid this with by adding an exists condition that filters out "unmatched" rows:
update customer up
set (customer_name, account, active) = (
select tmp.name, tmp.account, case when tmp.active = 'Yes' then 1 else 0 end
from customer_temp tmp
where up.agent = substr(tmp.agent, -4) and up.customer_code = tmp.code
)
where exists (
select 1
from customer_temp tmp
where up.agent = substr(tmp.agent, -4) and up.customer_code = tmp.code
);

Oracle Trigger with UPDATE OF ( column name identical with table)

i have a problem with my column who has the same name as one of my tables.
I can not use table.column in this situation
repres was a table in the windev database and a column in commerc database and devent table where i create this trigger
When i do : AFTER UPDATE OF etat,total, devent.repres
I Have :
ERROR line 2, col 35, ending_line 2, ending_col 35, Found '.', Expecting: , or ON OR
This is my code:
CREATE OR REPLACE TRIGGER WINDEV_AES_UPDATE
AFTER UPDATE OF etat,total, repres
ON COMMERC.DEVENT
FOR EACH ROW
DECLARE
BEGIN
IF UPDATING AND :OLD.etat != :NEW.etat THEN
INSERT INTO windev.aes (code)
SELECT (:NEW.CODE)
FROM DUAL
WHERE NOT EXISTS (SELECT * FROM windev.aes WHERE code = :NEW.CODE);
UPDATE windev.aes SET aes.update_flag = 1 WHERE aes.code = :NEW.CODE;
END IF;
IF UPDATING AND :OLD.total != :NEW.total THEN
INSERT INTO windev.aes (code)
SELECT (:NEW.CODE)
FROM DUAL
WHERE NOT EXISTS (SELECT * FROM windev.aes WHERE code = :NEW.CODE);
UPDATE windev.aes SET aes.update_flag = 1 WHERE aes.code = :NEW.CODE;
END IF;
END;
/
I hope you tell me how resolve this epic problem ;-)

Oracle sqlplus logging stops

This is strange...
I'm running a script in sqlplus. It seeems to have completed it's function, however when I review the .log it seems to have stopped logging...has anyone else had this experience?
I think I may have opened the logfile at some stage while the script was running to check process, perhaps that's no-no?
The redacted details are:
set serveroutput on;
set feedback off;
set echo on;
spool 4logfilethatstops.log
BEGIN
/*-----------------------------------------*/
/* Establishing AUID on dmt_identifier table*/
update DMT_CUSTOMER_ID
set auid=CUSTOMER_ID
where CUSTOMER_ID_ZONE_OFFICE = '&&var_DM_ID_ZONE_OFFICEority'
and batch_id = '&&var_batchid';
/*-----------------------------------------*/
/*Copying DM_ID to all tables*/
update dmt_ a
set DM_ID= (select CUSTOMER_ID from DMT_CUSTOMER_ID b
where b.PRI_CLT_RECORD_ID=a.PRI_CLT_RECORD_ID
and b.batch_id = '&&var_batchid'
and b.CUSTOMER_ID_ZONE_OFFICE = '&&var_DM_ID_ZONE_OFFICEority'
and b.CUSTOMER_ID_major_flag = 'Y'
and b.CUSTOMER_ID_type_code = '004')
where a.batch_id = '&&var_batchid';
[10 more similar statements]
/*-----------------------------------------*/
/*Exclude records with no DM_IDs*/
update dmt_address c
set c.batch_id = c.batch_id||'-EXCLUDE'
where c.batch_id = '&&var_batchid'
and c.pri_clt_record_id in
(SELECT pri_clt_record_id FROM dmt_
where DM_ID is NULL );
[10 more similar statements]
/*-----------------------------------------*/
/*Exclude merged records from dmt_ tables*/
update dmt_ c
set batch_id = batch_id||'-EXCLUDE'
where batch_id not like '%EXCLUDE%'
and c.pri_clt_record_id in
(select b.pri_clt_record_id from dmt_ a , dmt_ b
where a.container_sequence_number = b.container_sequence_number
and b.rpl_pri_clt_record_id = a.pri_clt_record_id
and a.rpl_pri_clt_record_id is null);
[1 more similar statement]
/*--------------------------------------------------------*/
/*Exclude dmt_insurance records from dmt_ that are not MRA*/
update dmt_insurance
set batch_id = batch_id||'-EXCLUDE'
where batch_id not like '%EXCLUDE%'
and client_insurance_record_id not like 'MRA-%';
/*-----------------------------------------------------*/
/*Populate dmt_demog with dmt_profile data*/
update dmt_staticdemographics a
set MSTATUS=(select CLIENT_ATTRIBUTE_CODE
from dmt_profile
where CLIENT_ATTRIBUTE_TYPE_CODE = 02
and batch_id = '&&var_batchid'
and PRI_CLT_RECORD_ID=a.PRI_CLT_RECORD_ID);
[4 more similar statements]
/*--------------------*/
/*Populate DM_* tables*/
/*----------------*/
/*inserting person*/
insert into dm_person
(CUSTOMER_ID, DOB,DOB_EST,SEX,
CREATED_DATE,LAST_UPDATE_DATE, batch_id)
select DM_ID, DATE_OF_BIRTH,DATE_OF_BIRTH_ESTIMATION_FLAG,
SEX_CODE,MSTATUS,INDIGENOUS,RELIGION,LANGUAGE
from dmt_staticdemographics
where DM_ID is not null
and batch_id = '&&var_batchid';
[3 more similar statements]
/*----------------------------------------------------------------------------------*/
-- inserting external identifiers
insert into DM_IDENTIFIER
(CUSTOMER_ID,IDENTIFIER_START_DATE,IDENTIFIER_END_DATE,IDENTIFIER_TYPE,ID_VALUE,CREATED_DATE,LAST_UPDATE_DATE, BATCH_ID, ZONE_OFFICEority)
select DM_ID,effective_start_date,effective_end_date, ext_clt_id_type_code, EXT_CLT_ID,SOURCE_CREATE_DATETIME, SOURCE_MODIFIED_DATETIME,
BATCH_ID, ext_clt_id_ZONE_OFFICE
from dmt_extidentifier
where DM_ID is not null
and batch_id = '&&var_batchid';
[4 more similar statements]
COMMIT;
END;
/

Trigger on one table field works for all table fields

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;

Resources