两个表的合并数据

时间:2014-03-28 06:04:22

标签: oracle plsql oracle11g

我想编写一个查询,查找两个表之间的差异,并将更新或新数据写入第三个表。我的两个表具有相同的列名。捕获更改的第三个表有额外的列称为注释。我想根据行修改插入注释,无论是新行还是更新行。

**TABLE1 (BACKUP)**
KEY,FIRST_NAME,LAST_NAME,CITY
1,RAM,KUMAR,INDIA
2,TOM,MOODY,ENGLAND
3,MOHAMMAD,HAFEEZ,PAKISTAN
4,MONIKA,SAM,USA
5,MIKE,PALEDINO,USA

**TABLE2 (CURRENT)**
KEY,FIRST_NAME,LAST_NAME,CITY
1,RAM,KUMAR,USA
2,TOM,MOODY,ENGLAND
3,MOHAMMAD,HAFEEZ,PAKISTAN
4,MONIKA,SAM,INDIA
5,MIKE,PALEDINO,USA
6,MAHELA,JAYA,SL



**TABLE3 (DIFFERENCE FROM TABLE2 TO TABLE1)**
KEY,FIRST_NAME,LAST_NAME,CITY,COMMENT
1,RAM,KUMAR,USA,UPDATE
4,MONIKA,SAM,INDIA,UPDATE
6,MAHELA,JAYA,SL,INSERT

表脚本

DROP TABLE TABLE1;
DROP TABLE TABLE2;
DROP TABLE TABLE3;


CREATE TABLE TABLE1
(
KEY NUMBER,
FIRST_NAME VARCHAR2(100),
LAST_NAME VARCHAR2(100),
CITY VARCHAR2(50)
);
/


CREATE TABLE TABLE2
(
KEY NUMBER,
FIRST_NAME VARCHAR2(100),
LAST_NAME VARCHAR2(100),
CITY VARCHAR2(50)
);
/


CREATE TABLE TABLE3
(
KEY NUMBER,
FIRST_NAME VARCHAR2(100),
LAST_NAME VARCHAR2(100),
CITY VARCHAR2(50),
COMMENTS VARCHAR2(200)
);
/

INSERT ALL 
INTO TABLE1
VALUES(1,'RAM','KUMAR','INDIA')
INTO TABLE1 VALUES(2,'TOM','MOODY','ENGLAND')
INTO TABLE1 VALUES(3,'MOHAMMAD','HAFEEZ','PAKISTAN')
INTO TABLE1 VALUES(4,'MONIKA','SAM','USA')
INTO TABLE1 VALUES(5,'MIKE','PALEDINO','USA')
SELECT 1 FROM DUAL;

/

INSERT ALL 
INTO TABLE2
VALUES(1,'RAM','KUMAR','USA')
INTO TABLE2 VALUES(2,'TOM','MOODY','ENGLAND')
INTO TABLE2 VALUES(3,'MOHAMMAD','HAFEEZ','PAKISTAN')
INTO TABLE2 VALUES(4,'MONIKA','SAM','INDIA')
INTO TABLE2 VALUES(5,'MIKE','PALEDINO','USA')
INTO TABLE2 VALUES(6,'MAHELA','JAYA','SL')

SELECT 1 FROM DUAL;

我使用merge语句来完成同样的事情。但我在合并声明中遇到了障碍,它突然出现错误“SQL错误:ORA-00905:缺少关键字 00905. 00000 - “缺少关键字”“我不明白错误在哪里。请帮忙

INSERT INTO TABLE3
SELECT KEY,FIRST_NAME,LAST_NAME,CITY,NULL AS COMMENTS FROM TABLE2
MINUS 
SELECT KEY,FIRST_NAME,LAST_NAME,CITY,NULL AS COMMENTS FROM TABLE1

;


MERGE INTO TABLE3 A
USING TABLE1 B
ON (A.KEY=B.KEY)
WHEN MATCHED THEN 
UPDATE SET A.COMMENTS='UPDATED'
WHEN NOT MATCHED THEN
UPDATE SET A.COMMENTS='INSERTED';

3 个答案:

答案 0 :(得分:1)

没有这样的WHEN NOT MATCHED THEN UPDATE子句,您应该使用WHEN NOT MATCHED THEN INSERT。有关详细信息,请参阅MERGE

答案 1 :(得分:0)

基本上MERGE强制操作:MATCHED = UPDATE(或DELETE),NOT MATCHED = INSERT。它在the docs

你可以做你想做的事,但你需要两个带有不同集合运算符的插入语句,

更新:

Insert into table3 
    table1 INTERSECT table2

对于INSERTED:

Insert into table3 
    table2 MINUS table1

答案 2 :(得分:0)

对数据做了一些假设:

  • INSERT事件将是由table2(当前数据)中的密钥标识的记录,该记录在原始备份表中没有匹配的密钥:table1。< / LI>
  • UPDATE事件是table1table2中同一个KEY存在但不相同的字段。
  • 表格之间没有变化的记录不会记录在table3中。

示例查询:检查更新

 SELECT UPD_QUERY.NEW_CITY, 'UPDATED' as COMMENTS
   FROM (SELECT CASE WHEN REPLACE(CURR.CITY, BKUP.CITY,'') IS NOT NULL THEN CURR.CITY
                ELSE NULL END as NEW_CITY
           FROM table1 BKUP,  table2  CURR
          WHERE BKUP.KEY = CURR.KEY) UPD_QUERY
  WHERE UPD_QUERY.NEW_CITY is NOT NULL;

Updated City Data

您可以对其他字段重复此比较方法:

      SELECT UPD_QUERY.*
        FROM (SELECT CURR.KEY, 
                CASE WHEN REPLACE(CURR.FIRST_NAME, BKUP.FIRST_NAME,'') IS NOT NULL
                     THEN CURR.FIRST_NAME
                     ELSE NULL END as FIRST_NAME,

                CASE WHEN REPLACE(CURR.LAST_NAME, BKUP.LAST_NAME,'') IS NOT NULL
                     THEN CURR.LAST_NAME
                     ELSE NULL END as LAST_NAME,

                CASE WHEN REPLACE(CURR.CITY, BKUP.CITY,'') IS NOT NULL 
                     THEN CURR.CITY
                     ELSE NULL END as CITY

                FROM table1 BKUP, table2 CURR
               WHERE BKUP.KEY = CURR.KEY) UPD_QUERY
     WHERE COALESCE(UPD_QUERY.FIRST_NAME, UPD_QUERY.LAST_NAME, UPD_QUERY.CITY) 
           is NOT NULL;

注意:如果比较的列数很多,这可能会很快变得难以处理。由于目标表设计(table3)不仅需要识别更改,还会记录字段及其新值。


示例查询:查找新添加的记录

 SELECT CURR.*, 'INSERTED' as COMMENTS
   FROM table2 CURR, table1 BKUP
  WHERE CURR.KEY = BKUP.KEY(+)
    AND BKUP.KEY is NULL;

Newly Inserted Records