简体   繁体   中英

Compare the same multiple columns of two table in Oracle DB

I have two tables table 1 and table 2 ,have common columns(a,b,c and d). What requirement is I need to match all these 4 columns(all the 4 columns should match) . If these match then Job will run and if not job will not run.

This is how I understood the question.

First of all, you should have specified database you use; PL/SQL means "Oracle".


Sample tables:

SQL> select * From table1;

         A          B          C          D
---------- ---------- ---------- ----------
         1         10        100       1000
         2         20        200       2000

SQL> select * From table2;

         A          B          C          D
---------- ---------- ---------- ----------
         1         10        100          1     --> column D is different
         2         20        200       2000

The following query uses set operators and checks whether there are any rows that exist in table1 and don't exist in table2 and vice versa. If so, it means that the aren't equal and query won't return any rows (so you wouldn't run that "job").

SQL> select dummy
  2  from dual
  3  where not exists ((select a, b, c, d from table1
  4                     minus
  5                     select a, b, c, d from table2
  6                    )
  7                    union all
  8                    (select a, b, c, d from table2
  9                     minus
 10                     select a, b, c, d from table1
 11                    )
 12                   );

no rows selected

OK; let's now make tables equal and try again:

SQL> update table2 set d = 1000 where a = 1;

1 row updated.

SQL> select * From table1;

         A          B          C          D
---------- ---------- ---------- ----------
         1         10        100       1000
         2         20        200       2000

SQL> select * From table2;

         A          B          C          D
---------- ---------- ---------- ----------
         1         10        100       1000      --> column D is now equal to table1's D column
         2         20        200       2000

Query result:

SQL> select dummy
  2  from dual
  3  where not exists ((select a, b, c, d from table1
  4                     minus
  5                     select a, b, c, d from table2
  6                    )
  7                    union all
  8                    (select a, b, c, d from table2
  9                     minus
 10                     select a, b, c, d from table1
 11                    )
 12                   );

D
-
X          --> right; something is returned so - run the job!

SQL>

As this is a PL/SQL problem , just put such a query into a procedure:

SQL> create or replace procedure p_run is
  2    l_dummy dual.dummy%type;
  3  begin
  4    select dummy
  5    into l_dummy
  6    from dual
  7    where not exists ((select a, b, c, d from table1
  8                       minus
  9                       select a, b, c, d from table2
 10                      )
 11                      union all
 12                      (select a, b, c, d from table2
 13                       minus
 14                       select a, b, c, d from table1
 15                      )
 16                     );
 17    dbms_output.put_line('Run the job!');
 18  exception
 19    when no_data_found then
 20      dbms_output.put_line('Do nothing; tables aren''t equal');
 21  end;
 22  /

Procedure created.

Testing:

SQL> rollback;

Rollback complete.    

SQL> exec p_run;

Do nothing; tables aren't equal

PL/SQL procedure successfully completed.

SQL> -- let's make columns D equal as well
SQL> update table2 set d = 1000 where a = 1;

1 row updated.

SQL> exec p_run;
Run the job!

PL/SQL procedure successfully completed.

SQL>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM