Skip to Main Content
  • Questions
  • Comparining values between two tables

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, P.

Asked: August 16, 2019 - 6:25 pm UTC

Last updated: September 02, 2019 - 12:18 pm UTC

Version: 12c

Viewed 1000+ times

You Asked

Hi Tom

I'm trying to determine if its best to write this logic in SQL or should I write a PLSQL block.

My example:

I've two tables, one is main table which will have thousands of records and another is the report table which will have around 6000 records.
If few demographic field values changes in the main table for any of those 6000 records then those should be reported.

Table1 :
create table MAIN_CLIENT
(
    ID NUMBER(9,0),
    FRST_NME VARCHAR2(15 BYTE) NOT NULL ENABLE,
    MDD_NME VARCHAR2(15 BYTE),
    LST_NME VARCHAR2(20 BYTE) NOT NULL ENABLE
    ); 

Table 2:
create table MAIN_RPT
(
    ID NUMBER(9,0),
    FRST_NME VARCHAR2(15 BYTE) NOT NULL ENABLE,
    MDD_NME VARCHAR2(15 BYTE),
    LST_NME VARCHAR2(20 BYTE) NOT NULL ENABLE
    ); 

Insert into MAIN_CLIENT values (101,'MIKE','A','SMITH');
Insert into MAIN_RPT values (101,'MIKE','A','SMITH');


The main table got updated after we inserted records in the report table

Update MAIN_CLIENT set frst_nme = 'TIM' where id = 101;

My question:
I've PLSQL block which updates report table but I also need to check for existing values in the report table if its got upadated in the main table.
How to compare both the tables for changed values and query the record with new values.
Frst_NME, MDD_NME and LST_NME can be changed.

We can write PLSQL block but checking if its easier and efficient to do in SQL, Please suggest.

Thanks
P



and Connor said...

Stew Ashton did an excellent series of blog posts covering all the common methods.

I'll refer you to that

https://stewashton.wordpress.com/list-of-my-posts-about-comparing-and-synchronizing-tables/



Is this answer out of date? If it is, please let us know via a Comment

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library