Compare column of one table to another and update in Oracle - oracle

I have two tables for which I want to create a Stored procedure.
Table 1:- R4G_APPDEV.plannedroute_evw Table 2 :- ASSIGNED_ROUTE_MASTER
Now what I want is I want to compare some column from Table 2 with Table 1 and if there is any difference in the column values then update the column in Table 2.
Below are the Table description for both the tables.
Table 1:- R4G_APPDEV.plannedroute_evw
Name Null? Type
--------------------------- ----- ---------------
OBJECTID NUMBER
MEASUREDLENGTH NUMBER(38,8)
DEPTHPLACED NUMBER(38,8)
HEIGHT NUMBER(38,8)
WIDTH NUMBER(38,8)
NUMBEROFDUCTS NUMBER(5)
NUMBEROFSUBDUCTS NUMBER(5)
DUCTDIAMETER NUMBER(38,8)
SUBDUCTDIAMETER NUMBER(38,8)
REMARKS NVARCHAR2(250)
NETWORKTYPE NUMBER(10)
NETWORKCATEGORY NVARCHAR2(50)
ROUTENAME NVARCHAR2(200)
FACILITYID NVARCHAR2(41)
BWLEASE NVARCHAR2(50)
ENABLED NUMBER(5)
TRENCHMATERIAL NVARCHAR2(15)
UNITS NVARCHAR2(5)
AGGREGATEROUTENAME NVARCHAR2(200)
CSSROUTENAME NVARCHAR2(200)
COREROUTENAME NVARCHAR2(200)
PHASE NVARCHAR2(20)
PATHID NVARCHAR2(30)
AGGREGATEROUTE NVARCHAR2(50)
OWNER NVARCHAR2(50)
ACTUALLENGTH NUMBER(38,8)
CITYCODE NVARCHAR2(4)
CITYNAME NVARCHAR2(50)
DUCTCOLOUR NVARCHAR2(50)
REGULATORYDUCT NUMBER(5)
ADDITIONALPROTECTIONREQD NVARCHAR2(10)
PROTECTIONTYPE NVARCHAR2(200)
LINK_ID NVARCHAR2(25)
SPAN_ID NVARCHAR2(25)
OPERATOR_ID NVARCHAR2(50)
CONSTRUCTION_METHODOLOGY NVARCHAR2(50)
ROUTEPATH_NEW NVARCHAR2(200)
ROUTETYPE NVARCHAR2(50)
SWITH_ON_CORE NVARCHAR2(50)
FINAL_CORE NVARCHAR2(50)
SWITCH_ON_COLLECTOR NVARCHAR2(50)
FINAL_COLLECTOR NVARCHAR2(50)
COLLECTOR_BW_CLASSIFICATION NVARCHAR2(50)
FROM_CITY NVARCHAR2(50)
TO_CITY NVARCHAR2(50)
PLANNEDDATE TIMESTAMP(6)
USER_NAME NVARCHAR2(50)
CREATED_USER NVARCHAR2(50)
LAST_EDITED_USER NVARCHAR2(50)
LAST_EDITED_DATE TIMESTAMP(6)
CREATED_DATE TIMESTAMP(6)
INTRACITY_LINK_ID NVARCHAR2(20)
SRNO NUMBER(10)
LEASE_ROUTE_NAME NVARCHAR2(200)
R4G_STATE NVARCHAR2(50)
LEASE_ROUTE_LINK_ID NVARCHAR2(25)
LEASE_ROUTE_SPAN_ID NVARCHAR2(25)
SCOPE_CATEGORY NVARCHAR2(25)
INTRACITY_CONSTRUCTION_TYPE NVARCHAR2(25)
FACILITY_SAP_ID NVARCHAR2(25)
NLD_STAGES NVARCHAR2(10)
AG_PARENTID NVARCHAR2(30)
CORE_PARENTID_1 NVARCHAR2(30)
CORE_PARENTID_2 NVARCHAR2(30)
SDCC_OBLIGATION NVARCHAR2(25)
LASTMILE_ID NVARCHAR2(20)
LINK_ID_100G NVARCHAR2(25)
SPAN_ID_100G NVARCHAR2(25)
LINK_ID_NX10G NVARCHAR2(25)
SPAN_ID_NX10G NVARCHAR2(25)
LINK_ID_1X10G NVARCHAR2(25)
SPAN_ID_1X10G NVARCHAR2(25)
LINK_ID_1X1G NVARCHAR2(25)
SPAN_ID_1X1G NVARCHAR2(25)
LINK_STATUS NVARCHAR2(30)
LINK_RFE_DATE TIMESTAMP(6)
NLD_POP_PATH1 NVARCHAR2(25)
NLD_POP_PATH2 NVARCHAR2(25)
EPC_PATH1 NVARCHAR2(25)
EPC_PATH2 NVARCHAR2(25)
LINK_RFE_PLAN TIMESTAMP(6)
LINK_RFE_ACT TIMESTAMP(6)
LINK_RFE_FCST TIMESTAMP(6)
LAUNCH_MRO NVARCHAR2(50)
LAUNCH_FRO NVARCHAR2(50)
GLOBALID CHAR(38)
JIOCENTERNAME NVARCHAR2(255)
JIOCENTERCODE NVARCHAR2(10)
JIOCENTER_RJID NVARCHAR2(10)
LOGICAL_LINK_ID NVARCHAR2(25)
LM_PARENT_ID NVARCHAR2(25)
INTRACITY_PHASE NVARCHAR2(25)
OLD_SPAN_ID NVARCHAR2(21)
LM_CONNECTIVITY_TYPE NVARCHAR2(20)
PARENTEASTAG1SAPID NVARCHAR2(18)
PARENTWESTAG1SAPID NVARCHAR2(18)
POST_DATE TIMESTAMP(6)
SURVEYSPANID_1 NVARCHAR2(21)
SURVEYSPANID_2 NVARCHAR2(21)
BLOWINGSPANID_2ND_DUCT NVARCHAR2(21)
BLOWINGSPANID_3RD_DUCT NVARCHAR2(21)
BLOWING_DUCT_COLOUR NVARCHAR2(15)
OLD_INTRACITY_LINK_ID NVARCHAR2(15)
HOTO_DONE_DATE TIMESTAMP(6)
HOTO_DONE NVARCHAR2(50)
SHAPE SDE.ST_GEOMETRY
SDE_STATE_ID NUMBER
DESC Table 2:- R4G_APPDEV.plannedroute_evw
Name Null? Type
------------------------ -------- --------------
IID NOT NULL VARCHAR2(50)
PROJECT_PHASE VARCHAR2(20)
NETWORK_CATEGORY VARCHAR2(50)
NETWORK_TYPE NUMBER
ROUTE_NAME VARCHAR2(200)
LINK_ID VARCHAR2(25)
SPAN_ID VARCHAR2(25)
SPAN_NAME VARCHAR2(200)
PARENT_LINK_ID VARCHAR2(25)
SCOPE_CATEGORY VARCHAR2(25)
FROM_FACILITY_LAT NUMBER
FROM_FACILITY_LONG NUMBER
TO_FACILITY_LAT NUMBER
TO_FACILITY_LONG NUMBER
FROM_FACILITY VARCHAR2(25)
TO_FACILITY VARCHAR2(25)
LENGTH NUMBER
JIO_STATE_CODE VARCHAR2(4)
JIO_STATE_NAME VARCHAR2(200)
STATE_CODE VARCHAR2(4)
STATE_NAME VARCHAR2(200)
CITY_CODE VARCHAR2(4)
CITY_NAME VARCHAR2(200)
FROM_CITY_NAME VARCHAR2(200)
TO_CITY_NAME VARCHAR2(200)
FROM_CITY_CODE VARCHAR2(4)
TO_CITY_CODE VARCHAR2(4)
NO_OF_CROSSING NUMBER
TYPE_OF_CROSSING VARCHAR2(100)
EXECUTING_MP VARCHAR2(50)
STATUS VARCHAR2(40)
DATE_ROUTE_RELEASED DATE
LAST_EDITED_USER VARCHAR2(100)
LAST_EDITED_DATE NOT NULL DATE
INTRACITY_LINK_ID VARCHAR2(25)
WORK_ORDER_NAME VARCHAR2(50)
WORK_ORDER_STATUS_CODE VARCHAR2(20)
WORKFLOW VARCHAR2(20)
LAST_EDITED_USER_REMARKS VARCHAR2(1000)
OBJECTID NOT NULL NUMBER(38)
REVISION NUMBER
TND_SCOPE_LENGTH NUMBER(20,2)
BLOWING_SCOPE_LENGTH NUMBER(20,2)
SAP_ID VARCHAR2(20)
LASTMILE_ID VARCHAR2(20)
Please let me know on how to update it.
**Note ** Table 1 unique column is OBJECTID and in table 2 it is IID
UPDATE
I started writing my procedure like below for comparing one column. Now I want to loop it for every records and if find any difference then update it. How to do it
CREATE OR REPLACE PROCEDURE PROC_CHK_PLNDRT_ASSMST AS
VACTUALLEN nvarchar2(50);
VLENGTH nvarchar2(50);
BEGIN
SELECT ACTUALLENGTH into VACTUALLEN from R4G_APPDEV.plannedroute_evw
UNION ALL
select LENGTH into VLENGTH from ASSIGNED_ROUTE_MASTER;
NULL;
END PROC_CHK_PLNDRT_ASSMST;

The way I understood it, you don't actually need to compare column-to-column and do the update if something's different - update it unconditionally (makes the "procedure" simpler). It means that you don't have to use PL/SQL at all as merge handles it at SQL level.
Tables' descriptions are different and I don't know which column from plannedroute_evw maps to which column in assigned_route_master. But, if you do, you'll easily complete the following merge statement.
Here's how:
merge into assigned_route_master b
using plannedroute_evw a
on (a.objectid = b.iid)
when matched then update set
b.length = a.actual_length,
b.city_code = a.city_code,
<here you should map all columns between these two tables>
;

Related

Create a trigger on table whenever a column is inserted or updated

I have a table whose description is below
Name Null Type
----------------------------- -------- --------------
ID NOT NULL NUMBER
CMP NVARCHAR2(100)
SAP_ID NVARCHAR2(100)
ID_OD_COUNTCHANGE NVARCHAR2(100)
ID_OD_CHANGEDDATE DATE
RRH_COUNTCHANGE NVARCHAR2(100)
RRH_CHANGEDDATE DATE
TENANCY_COUNTCHANGE NVARCHAR2(100)
TENANCY_CHANGEDDATE DATE
RFS_DATE DATE
RFE1_DATE DATE
INFRA_PROVIDER NVARCHAR2(100)
IP_COLO_SITEID NVARCHAR2(100)
SITE_NAME NVARCHAR2(500)
R4GSTATE NVARCHAR2(100)
MW_INSTALLED NVARCHAR2(100)
DG_NONDG NVARCHAR2(100)
EB_NONEB NVARCHAR2(100)
TOWER_TYPE NVARCHAR2(100)
VENDOR_CODE NVARCHAR2(100)
RFCDATE DATE
POLITICAL_STATE_NAME NVARCHAR2(100)
POLITICAL_STATE_CODE NVARCHAR2(100)
SITE_DROP_DATE DATE
CITY_NAME NVARCHAR2(100)
NEID NVARCHAR2(100)
FACILITY_LATITUDE NVARCHAR2(100)
FACILITY_LONGITUDE NVARCHAR2(100)
RJ_STRUCTURE_TYPE NVARCHAR2(100)
RJ_JC_NAME NVARCHAR2(100)
RJ_JC_CODE NVARCHAR2(100)
COMPANY_CODE NVARCHAR2(100)
BLCHAIN_RESP_MSG_MASTER NVARCHAR2(100)
BLCHAIN_RESP_CODE_MASTER NVARCHAR2(100)
SITE_ADDRESS CLOB
BLCHAIN_RESP_MSG_INCREMENTAL VARCHAR2(100)
BLCHAIN_RESP_CODE_INCREMENTAL VARCHAR2(100)
CREATED_BY NVARCHAR2(100)
CREATED_DATE DATE
SEL_CHANGED_VAL NVARCHAR2(100)
CMM NVARCHAR2(20)
FCA NVARCHAR2(20)
LAST_UPDATED_BY NVARCHAR2(100)
LAST_UPDATED_DATE DATE
What I want is, I want to write a trigger that whenever any row is inserted or updated I want to update the LAST_UPDATED_DATE column by SYSDATE.
How can I create the trigger?
For Insert, You can use the DEFAULT clause in the table and for UPDATE, You can create the simple trigger as follows:
CREATE OR REPLACE TRIGGER TRG_NAME
BEFORE UPDATE ON YOUR_TABLE_NAME
FOR EACH ROW
BEGIN
:NEW.LAST_UPDATED_DATE := SYSDATE;
END;
/

Show only unmatched values of two columns in third column of table in an oracle

I want to create a stored procedure where I want to check the below condition.
In above two columns, RJ_SAPID and RJ_COLO_SAPID in first row the value I-GJ-JMLU-ENB-6011 is common.
So except that value i want to show the other two values in third column by using stored procedure.
I tried using REPLACE function but couldn't succeed. Below is the code
SELECT REPLACE(RJ_SAPID, RJ_COLO_SAPID, '') FROM NE_STRUCTURES;
Please suggest how to proceed.
table definition
OBJECTID NUMBER
RJ_SAPID VARCHAR2(20)
RJ_COLO_SAPID VARCHAR2(200)
RJ_NETWORK_ENTITY_ID VARCHAR2(30)
STRUCTURE_NAME VARCHAR2(200)
INVENTORY_STATUS_CODE VARCHAR2(4)
RJ_MAINTENANCE_ZONE_CODE VARCHAR2(20)
RJ_SITE_NAME VARCHAR2(200)
RJ_SITE_ADDRESS VARCHAR2(500)
RJ_STRUCTURE_TYPE VARCHAR2(20)
TYPE_NAME VARCHAR2(20)
RJ_LAST_MODIFIED_BY VARCHAR2(50)
RJ_LAST_MODIFIED_DATE DATE
RJ_STATUS VARCHAR2(200)
RJ_CITY_CODE VARCHAR2(10)
RJ_R4G_STATE_CODE VARCHAR2(10)
RJ_DISTRICT_CODE VARCHAR2(20)
RJ_TALUK_CODE VARCHAR2(20)
RJ_JC_CODE VARCHAR2(20)
RJ_JIOPOINT_SAPCODE VARCHAR2(20)
RJ_COMPANY_CODE_1 VARCHAR2(20)
RJ_COMPANY_CODE_2 VARCHAR2(20)
NE_STATUS VARCHAR2(20)
You could use REPLACE(RJ_COLO_SAPID, RJ_SAPID, '') and for leading comma, use LTRIM
SELECT RJ_SAPID, RJ_COLO_SAPID,
REPLACE(RTRIM(LTRIM(REPLACE(RJ_COLO_SAPID, RJ_SAPID, ''), ','), ','), ',,', '') AS ThirdColumn
FROM NE_STRUCTURES
Just switch your arguments and you will be fine:
SELECT REPLACE(RJ_COLO_SAPID, RJ_SAPID, '') FROM NE_STRUCTURES
And if you want to strip unneeded commata use:
SELECT REGEXP_REPLACE(REPLACE(RJ_COLO_SAPID, RJ_SAPID, ''),'^,+|,+$|(,),+','\1') FROM NE_STRUCTURES

PLS-00225: subprogram or cursor 'CHR' reference is out of scope

i make a procedure in plsql it return this error anyone know about it i post my code and error snap shot here
CREATE OR REPLACE PROCEDURE CHR.ATT_INSERT_TEST
IS
CURSOR ATT
IS
SELECT emp.EMPLOYEE_ID EMPLOYEE_ID,
io.CHECKTYPE CHECKTYPE,
io.MACHINE_NUM MACHINE_NUM,
io.ATT_ID ATT_ID,
io.SWIPE_DATE SWIPE_DATE,
io.SWIPE_TIME SWIPE_TIME
FROM INOUT_LIVE_MACHINE_TEST io, CHR_EMGT_EMPLOYEE emp
WHERE emp.EMPLOYEE_CODE = io.EMPLOYEE_CODE
AND io.ATT_ID NOT IN (SELECT ATT_ID_REF
FROM CHR_TA_EMP_SWIPE_IN_OUT
WHERE io.ATT_ID = ATT_ID_REF);
BEGIN
FOR I IN ATT
LOOP
INSERT INTO CHR_TA_EMP_SWIPE_IN_OUT (EMPLOYEE_ID,
SWIPE_DATE,
SWIPE_TIME,
SWIPE_ID,
SWIPE_TYPE,
CREATED_BY,
CREATION_DATE,
CLIENT_IP)
VALUES (I.EMPLOYEE_ID,
TO_DATE (i.SWIPE_DATE, 'MM/DD/YYYY'),
I.SWIPE_TIME,
CHR_TA_SWIPE_IN_OUT_SEQ.NEXTVAL,
I.CHECKTYPE,
I.EMPLOYEE_ID,
TO_DATE (i.SWIPE_DATE, 'MM/DD/YYYY'),
'192.168.0.71'
);
END LOOP;
COMMIT;
END;
/
this is my procedure i make subquery mostly time but this time it return me this kind of error.
i think this error show some issue is regarding alias but i already define alias for each column
i read this artical but my code is still stuck
PLS-00402: alias required in SELECT list of cursor to avoid duplicate column names
this is my table structure
CREATE TABLE CHR.CHR_TA_EMP_SWIPE_IN_OUT
(
EMPLOYEE_ID NUMBER(10) NOT NULL,
SWIPE_DATE DATE NOT NULL,
SWIPE_TIME DATE NOT NULL,
SWIPE_ID NUMBER(10) NOT NULL,
SWIPE_TYPE VARCHAR2(1 BYTE),
CREATED_BY NUMBER(10),
CREATION_DATE DATE,
LAST_UPDATED_BY NUMBER(10),
LAST_UPDATE_DATE DATE,
IS_MANUAL VARCHAR2(1 BYTE),
REASON_LKP NUMBER(10),
CLIENT_IP VARCHAR2(50 BYTE),
IS_REQUESTED VARCHAR2(1 CHAR),
TERMINAL_ID VARCHAR2(50 BYTE),
ATT_ID_REF NUMBER
)
CREATE TABLE CHR.CHR_EMGT_EMPLOYEE
(
EMPLOYEE_ID NUMBER(10) NOT NULL,
EMPLOYEE_CODE VARCHAR2(30 BYTE) NOT NULL
)
CREATE TABLE CHR.INOUT_LIVE_MACHINE_TEST
(
ATT_ID NUMBER,
USERID NUMBER,
EMPLOYEE_CODE NUMBER,
SENSORID NUMBER,
MACHINE_NUM VARCHAR2(20 BYTE),
CHECKTIME VARCHAR2(20 BYTE),
CHECKTYPE VARCHAR2(20 BYTE)
)
Your procedure has no mistakes. Only problem is in your table "INOUT_LIVE_MACHINE_TEST". it has missing SWIPE_DATE & SWIPE_TIME columns. See below the working demo.
Created table with both the columns:
CREATE TABLE INOUT_LIVE_MACHINE_TEST
(
ATT_ID NUMBER,
USERID NUMBER,
EMPLOYEE_CODE NUMBER,
SENSORID NUMBER,
SWIPE_DATE DATE NOT NULL,
SWIPE_TIME DATE NOT NULL,
MACHINE_NUM VARCHAR2(20 BYTE),
CHECKTIME VARCHAR2(20 BYTE),
CHECKTYPE VARCHAR2(20 BYTE)
)
Procedure compiled :
CREATE OR REPLACE PROCEDURE ATT_INSERT_TEST
IS
CURSOR ATT
IS
SELECT emp.EMPLOYEE_ID EMPLOYEE_ID,
io.CHECKTYPE CHECKTYPE,
io.MACHINE_NUM MACHINE_NUM,
io.ATT_ID ATT_ID,
io.SWIPE_DATE SWIPE_DATE,
io.SWIPE_TIME SWIPE_TIME
FROM INOUT_LIVE_MACHINE_TEST io, CHR_EMGT_EMPLOYEE emp
WHERE emp.EMPLOYEE_CODE = io.EMPLOYEE_CODE
AND io.ATT_ID NOT IN (SELECT ATT_ID_REF
FROM CHR_TA_EMP_SWIPE_IN_OUT
WHERE io.ATT_ID = ATT_ID_REF);
BEGIN
FOR I IN ATT
LOOP
INSERT INTO CHR_TA_EMP_SWIPE_IN_OUT (EMPLOYEE_ID,
SWIPE_DATE,
SWIPE_TIME,
SWIPE_ID,
SWIPE_TYPE,
CREATED_BY,
CREATION_DATE,
CLIENT_IP)
VALUES (I.EMPLOYEE_ID,
TO_DATE (i.SWIPE_DATE, 'MM/DD/YYYY'),
I.SWIPE_TIME,
CHR_TA_SWIPE_IN_OUT_SEQ.NEXTVAL,
I.CHECKTYPE,
I.EMPLOYEE_ID,
TO_DATE (i.SWIPE_DATE, 'MM/DD/YYYY'),
'192.168.0.71'
);
END LOOP;
COMMIT;
END;
/

Missing Right Parenthesis while using sequence

CREATE TABLE customer_master(
cust_id NUMBER(6) CUSTOMER_SEQ.Nextval PRIMARY KEY,
cust_password VARCHAR2(10),
cust_fname VARCHAR2(30),
cust_lname VARCHAR2(30),
cust_gender VARCHAR2(8),
cust_dob DATE,
cust_email VARCHAR2(30),
cust_password VARCHAR2(30),
cust_mobile NUMBER(10),
cust_add VARCHAR2(100),
newsletter VARCHAR2(5) CHECK (newsletter IN('yes','no')),
mob_service VARCHAR2(5) CHECK (mob_service IN('yes','no'))
);
Try to use trigger.
CREATE TABLE customer_master(
cust_id NUMBER(6) PRIMARY KEY,
cust_password VARCHAR2(10),
cust_fname VARCHAR2(30),
cust_lname VARCHAR2(30),
cust_gender VARCHAR2(8),
cust_dob DATE,
cust_email VARCHAR2(30),
cust_mobile NUMBER(10),
cust_add VARCHAR2(100),
newsletter VARCHAR2(5) CHECK (newsletter IN('yes','no')),
mob_service VARCHAR2(5) CHECK (mob_service IN('yes','no'))
);
CREATE OR REPLACE TRIGGER INS_customer_master
BEFORE INSERT ON customer_master
FOR EACH ROW
DECLARE
i_LAST NUMBER;
BEGIN
SELECT CUSTOMER_SEQ.NEXTVAL INTO i_LAST FROM Dual;
:NEW.cust_id := i_LAST;
END;
/

Update CLOB Column to NULL

I need to update an oracle clob column value to null. I tried to below but that didn't work. Any ideas?
update table_name
set CONTENT_TEMPLATE=empty_clob()
where table_key=12345;
Name Null Type
-------------------- -------- -------------
TEST_KEY NOT NULL NUMBER(10)
TEST_ID NOT NULL VARCHAR2(100)
TEST_TYPE NOT NULL VARCHAR2(30)
TEMPLATE_ID VARCHAR2(100)
ROUTE VARCHAR2(100)
MEDIUM VARCHAR2(100)
BEGIN_EFFECTIVE_DATE NOT NULL DATE
END_EFFECTIVE_DATE NOT NULL DATE
CUSTOMER_ID NOT NULL VARCHAR2(30)
VAR_ID NOT NULL VARCHAR2(30)
CREATION_DATE NOT NULL DATE
LAST_UPDATED_DATE NOT NULL DATE
LAST_UPDATED_BY NOT NULL VARCHAR2(30)
CONTENT_TEMPLATE CLOB
SUBJECT_TEMPLATE CLOB
UNIT_TYPE VARCHAR2(30)
OVERLOADED_ALERT_KEY NUMBER(10)
ALERT_TYPE_ALIAS VARCHAR2(100)
update table_name
set column_name = null
where table_key=12345;
NULL - Absense of data
empty_clob() - CLOB is initialized and empty, not same as NULL.
If you still get ORA-01407 with this column then that means there is a NOT NULL constraint on the column which is of CLOB datatype.
UPDATE:
Since the CLOB column is not NOT NULL setting it to CONTENT_TEMPLATE = NULL should work. CONTENT_TEMPLATE = empty_clob() will also work, but bear in mind that it is not same as NULL.

Resources