Compare columns in two tables - oracle

In ODI mapping, how can I compare source and target tables? I need to compare col1 (the source table) with col1 (the target table) row by row. If the target table differs from source, then update the target table (the same column and same row), else check the next row. Which component should I use and how?

Try with the below sample:
table1: Target table
table2: Source table
MERGE INTO table1 t1 USING table t2 ON (t1.id = t2.id)
WHEN MATCHED THEN UPDATE SET t1.value = t2.value
WHEN NOT MATCHED THEN INSERT (id, value) values (t2.id, t2.value);

Related

How to add one column data to another table column using joins or any other method?

There are two tables one is COPYCLAIMPRGNOTE and another one is COPYCLAIMPRGNOTE1, COPYCLAIMPRGNOTE contain only 2 columns one is claim_id and another one is NOTE_MEMO_CARETECH. and other table have many columns but NOTE_MEMO_CARETECH column is missing. I want to create NOTE_MEMO_CARETECH column in COPYCLAIMPRGNOTE1 with all the values which is in another table COPYCLAIMPRGNOTE.
How to do it?
I tried this below code but both not working.
INSERT INTO COPYCLAIMPRGNOTE1 (CLAIM_ID,NOTE_MEMO_CARETECH)
SELECT DISTINCT COPYCLAIMPRGNOTE1.CLAIM_ID,COPYCLAIMPRGNOTE1.NOTE_MEMO_CARETECH
FROM COPYCLAIMPRGNOTE src
inner JOIN COPYCLAIMPRGNOTE1
ON (COPYCLAIMPRGNOTE.CLAIM_ID=COPYCLAIMPRGNOTE1.CLAIM_ID)
insert into
COPYCLAIMPRGNOTE1 values (NOTE_MEMO_CARETECH)
INNER JOIN COPYCLAIMPRGNOTE ON
COPYCLAIMPRGNOTE1.CLAIM_ID = COPYCLAIMPRGNOTE.CLAIM_ID
ORDER BY
EVENT_ID DESC;
UPDATE
(
SELECT
NOTE_MEMO_CARETECH_OLD
FROM COPYCLAIMPRGNOTE1
INNER JOIN COPYCLAIMPRGNOTE
ON CLAIM_ID = CLAIM_ID
)
SET NOTE_MEMO_CARETECH_OLD = NOTE_MEMO_CARETECH
Two steps, because the target table doesn't even contain that column. DML can't create it, so you'll need DDL. Datatype should match the source table column's datatype (just as an example, I used varchar2(20)):
alter table copyclaimprgnote1 add note_memo_caretech varchar2(20);
The second step is the update (or merge, as in this example):
merge into copyclaimprgnote1 a
using copyclaimprgnote b
on (a.claim_id = b.claim_id)
when matched then update set
a.note_memo_caretech = b.note_memo_caretech;

How to copy all constrains and data form one schema to another in oracle

I am using Toad for oracle 12c. I need to copy a table and data (40M) from one shcema to another (prod to test). However there is an unique key(not the PK for this table) called record_Id col which has something data like this 3.000*******19E15. About 2M rows has same numbers(I believe its because very large number) which are unique in prod. When I try to copy it violets the unique key of that col. I am using toad "export data to another schema" function to copy the data.
when I execute query in prod
select count(*) from table_name
OR
select count(distinct(record_id) from table_name
Both query gives the exact same numbers of data.
I don't have DBA permission. How do I copy all data without violating unique key of the table.
Thanks in advance!
You can use UPSERT for decisional INSERT or UPDATE or you may write small procedure for this.
you may consider to use NOT EXISTS, but your data is big and it might not be resource efficient.
insert into prod_tab
select * from other_tab t1 where NOT exists (
select 1 from prod_tab t2 where t1.id = t2.id
);
In Oracle you can use a MERGE query for that.
The following query proceeds as follows for each data row :
if the source record_id does not yet exist in the target table, a new record is inserted
else, the existing record is updated with source values
For the sake of the example, I assumed that there are two other columns in the table : column1 and column2.
MERGE INTO target_table t1
USING (SELECT * from source_table t2)
ON (t1.record_id = t2.record_id)
WHEN MATCHED THEN UPDATE SET
t1.column1 = t2.column1,
t1.column2 = t2.column2
WHEN NOT MATCHED THEN INSERT
(record_id, column1, column2) VALUES (t2.record_id, t2.column1, t2.column2)

Using "contains" as a way to join tables?

The Primary key in table one is used as in table 2 but it is modified as so:
Primary key Column in table 1: 123abc
Column in table 2: 123abc_1
I.e. the key is used but then _1 is added to create a unique value in the column of Table 2.
Is there any way that I can join the two tables, the data in the 2 columns is not identical but it very similar. Could I do something like:
SELECT *
FROM TABLE1 INNER JOIN
TABLE2
ON TABLE1.COUMN1 contains TABLE2.COLUMN2;
I.e. checking that the value in Table 1 is within the value in Table 2?
You can check only the first part of column2; for example
SELECT *
FROM TABLE1 INNER JOIN TABLE2
ON INSTR(COLUMN2, COLUMN1) = 1
or
ON COLUMN2 LIKE COLUMN1 || '%'
However, keeping foreign key in such a way can be really dangerous, not to think about performance on large DBs.
You'd better use a different column in Table2 to store the key of Table 1, even adding a constraint.

sql statement to select and insert one value from one table rest from outside the tables

I have two db2 tables, table one contains rows of constant data with id as one of the columns. second table contains id column which is foreign key to id column in first table and has 3 more varchar columns.
I am trying to insert rows into second table, where entry into id col is based on a where clause and the remaining columns get values from outside.
table1 has columns id, t1c1, t1c2, t1c3
table2 has columns id, t2c1, t2c2, t2c3
to insert into table2, I am trying this query:
insert into table2 values
(select id from table1 where t1c2 like 'xxx', 'abc1','abc2');
I know it is something basic I am missing here.
Please help correcting this query.

How to populate column data in one table based on an id match from another table (using the data from the matched table)

In Oracle I have two tables. Both are populated with data and have a timestamp column.
One table has that column filled with data, the other table doesn't. But I need to get the data from the table that does into the column of the other table based on a match of another column.
Each has 'code' so the timestamp from the one table should only be put in the timestamp of the other table where the codes match.
I've tried cursors etc, but it seems like I'm missing something.
Any suggestions?
It sounds like you want a correlated update. This will update every row of destinationTable with the timestamp_col from sourceTable where there is a match on the code column.
UPDATE destinationTable d
SET timestamp_col = (SELECT s.timestamp_col
FROM sourceTable s
WHERE s.code = d.code )
WHERE EXISTS( SELECT 1
FROM sourceTable s
WHERE s.code = d.code )
Just for completeness, I think I would have done something like this (untested), if the destination has a primary key or unique column:
UPDATE (
SELECT d.primary_key AS pk, d.timestamp_col AS dest_col, MAX(s.timestamp_col) AS src_col
FROM dest d
JOIN src s ON d.code = s.code
GROUP BY pk, dest_col
) j
SET j.dest_col = j.src_col

Resources