我是 plsql 的新手,正在努力编写代码。我想使用 plsql 函数比较两个表(除了记录之外,两个表完全相同)。输入参数将是 (table1, table2) - 它应该比较 table1 和 table2 并返回三个输出。
- 表之间的任何不匹配记录
- table1 中存在但 table2 中不存在的任何行。
- table2 中存在但 table1 中不存在的任何行。
我用 SQL 编写了查询,它可以工作,但我在这里硬编码了一个特定的表名。请告知如何转换为函数。
SET TERMOUT OFF CONCAT OFF VERIFY OFF
SET FEEDBACK OFF
SET MARKUP HTML ON SPOOL ON
SPOOL ACTION.HTML
set pages 1000 lines 125
----
PROMPT SHOWING THE DIFFERENCE WITH THE MATCHING RECORDS FOR TABLE ACTION
BREAK ON ACTIONID duplicates skip page
column tblname format a10
select ACTIONID,tblname "tblname",ACTIONDESC,ACTIONNAME
from ((SELECT 'OLD VALUES' tblname,ACTIONID,ACTIONDESC,ACTIONNAME
FROM 'DEPL_ACTION
MINUS
SELECT 'NEW VALUES' tblname,ACTIONID,ACTIONDESC,ACTIONNAME
FROM ACTION )
UNION ALL
( SELECT 'NEW VALUES' tblname,ACTIONID,ACTIONDESC,ACTIONNAME
FROM ACTION
MINUS
SELECT 'OLD VALUES' tblname,ACTIONID,ACTIONDESC,ACTIONNAME
FROM 'DEPL_ACTION )) aa where (ACTIONID) in ( select ACTIONID from (
(SELECT ACTIONID,ACTIONDESC,ACTIONNAME
FROM 'DEPL_ACTION
MINUS
SELECT ACTIONID,ACTIONDESC,ACTIONNAME
FROM ACTION )
UNION ALL
( SELECT ACTIONID,ACTIONDESC,ACTIONNAME
FROM ACTION
MINUS
SELECT ACTIONID,ACTIONDESC,ACTIONNAME
FROM 'DEPL_ACTION ) ) group by ACTIONID having count(*) > 1 ) order by ACTIONID;
CLEAR BREAKS
PROMPT SHOWING THE EXTRA RECORDS IN OLD VALUES FOR TABLE ACTION
select ACTIONID,tblname "tblname",ACTIONDESC,ACTIONNAME
from ((SELECT 'OLD VALUES' tblname,ACTIONID,ACTIONDESC,ACTIONNAME
FROM 'DEPL_ACTION
MINUS
SELECT 'NEW VALUES' tblname,ACTIONID,ACTIONDESC,ACTIONNAME
FROM ACTION )
UNION ALL
( SELECT 'NEW VALUES' tblname,ACTIONID,ACTIONDESC,ACTIONNAME
FROM ACTION
MINUS
SELECT 'OLD VALUES' tblname,ACTIONID,ACTIONDESC,ACTIONNAME
FROM 'DEPL_ACTION )) aa where (ACTIONID) in ( select ACTIONID from (
(SELECT ACTIONID,ACTIONDESC,ACTIONNAME
FROM 'DEPL_ACTION
MINUS
SELECT ACTIONID,ACTIONDESC,ACTIONNAME
FROM ACTION )
UNION ALL
( SELECT ACTIONID,ACTIONDESC,ACTIONNAME
FROM ACTION
MINUS
SELECT ACTIONID,ACTIONDESC,ACTIONNAME
FROM 'DEPL_ACTION ) ) group by ACTIONID having count(*) = 1 ) order by ACTIONID;
PROMPT SHOWING THE EXTRA RECORDS IN NEW VALUES FOR TABLE ACTION
select ACTIONID,tblname "tblname",ACTIONDESC,ACTIONNAME
from ((SELECT 'OLD VALUES' tblname,ACTIONID,ACTIONDESC,ACTIONNAME
FROM 'DEPL_ACTION
MINUS
SELECT 'NEW VALUES' tblname,ACTIONID,ACTIONDESC,ACTIONNAME
FROM ACTION )
UNION ALL
( SELECT 'NEW VALUES' tblname,ACTIONID,ACTIONDESC,ACTIONNAME
FROM ACTION
MINUS
SELECT 'OLD VALUES' tblname,ACTIONID,ACTIONDESC,ACTIONNAME
FROM 'DEPL_ACTION )) aa where (ACTIONID) in ( select ACTIONID from (
(SELECT ACTIONID,ACTIONDESC,ACTIONNAME
FROM 'DEPL_ACTION
MINUS
SELECT ACTIONID,ACTIONDESC,ACTIONNAME
FROM ACTION )
UNION ALL
( SELECT ACTIONID,ACTIONDESC,ACTIONNAME
FROM ACTION
MINUS
SELECT ACTIONID,ACTIONDESC,ACTIONNAME
FROM 'DEPL_ACTION ) ) group by ACTIONID having count(*) = 1 ) order by ACTIONID;
SET MARKUP HTML OFF
SPOOL OFF
EXIT