简体   繁体   English

使用程序比较oracle sql服务器中每列的两个表

[英]Comparing two tables each column in oracle sql server using procedure

I Need a procedure to compare two tables of same schema but diffe.net data我需要一个程序来比较两个具有相同模式但 diffe.net 数据的表

Table 1: dep_name,emp_name,sal
Table 2: dep_name,emp_name,sal,status

Here's one option (sample data in lines #1 - 10).这是一个选项(第 1 - 10 行中的样本数据)。

(Though, I'm not sure you know which database you use. Is it Oracle or Microsoft SQL Server? I presumed it is Oracle. Also, screenshot doesn't support what you described - there's no status column in table 2, but it exists in table 3 which is - I presume (again) - result you want). (虽然,我不确定你知道你使用的是哪个数据库。它是 Oracle 还是 Microsoft SQL 服务器?我推测它是 Oracle。另外,屏幕截图不支持你所描述的 - 表 2 中没有status列,但它存在于表 3 中——我认为(再次)——你想要的结果)。

SQL> with
  2  t1 (dep_name, emp_name, sal) as
  3    (select 'Public', 'xxxx', 20000 from dual union all
  4     select 'Drug'  , 'yyyy', 20000 from dual
  5    ),
  6  t2 (dep_name, emp_name, sal) as
  7    (select 'Drug'  , 'yyyy', 10000 from dual union all
  8     select 'Public', 'xxxx', 20000 from dual union all
  9     select 'Police', 'zzzz', 40000 from dual
 10    )
 11  select b.dep_name, b.emp_name, greatest(nvl(a.sal, 0), nvl(b.sal, 0)) sal,
 12    case when a.sal  = b.sal then 'Matched'
 13         when a.sal <> b.sal then 'Unmatched'
 14         else 'New'
 15    end status
 16  from t2 b left join t1 a on a.dep_name = b.dep_name
 17                          and a.emp_name = b.emp_name;

DEP_NA EMP_        SAL STATUS
------ ---- ---------- ---------
Public xxxx      20000 Matched
Drug   yyyy      20000 Unmatched
Police zzzz      40000 New

SQL>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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