用于将id插入具有相同信息的行的PL / SQL过程

时间:2017-01-15 14:00:04

标签: sql oracle plsql

我在Oracle表中有联系方式数据,如下所示......

INPUT

我想插入新列,为具有匹配信息的联系人分配相同的ID,即基于姓氏,名字和(电话和/或电子邮件)组

输出应如下所示

OUTPUT

我是这个论坛的新手,因此在发布问题时遇到格式问题,请参阅随附的图片以便于理解我的要求

寻找PL / SQL程序,在我们庞大的数据库中完成这项工作

2 个答案:

答案 0 :(得分:0)

它不是最有效/漂亮的代码,但它适用于您只需要序列号

的情况
DECLARE 
I NUMBER := 0;

BEGIN 
  ALTER TABLE TABLE 
  ADD ID NUMBER;
  COMMIT;

FOR REC IN (SELECT * FROM TABLE) 
  LOOP 
    IF(REC.ID IS NULL) THEN
      UPDATE TABLE T SET ID = I 
      WHERE (T.EMAIL = REC.EMAIL OR T.PHONE = REC.PHONE)
      AND ID IS NULL;
      COMMIT;
      I := I + 1;
    END IF;
  END LOOP;
END;

答案 1 :(得分:0)

由于您提供的数据的性质,我认为没有可能在一个SQL中执行此操作或更好地调整它。

创建表格并填充示例的脚本:

create table TAB_TEST(LAST_NAME VARCHAR2(200),
FIRST_NAME VARCHAR2(200),
PHONE NUMBER,
EMAIL VARCHAR2(200))
/
INSERT INTO TAB_TEST VALUES
('Ho','Kim', 2058371579, 'ABC@GMAIL.COM')
/
INSERT INTO TAB_TEST VALUES
('Ho','Kim', 4479940000, 'ABC@GMAIL.COM')
/
INSERT INTO TAB_TEST VALUES
('Ho','Kim', 7195739945, 'XYZ@HOTMAIL.COM')
/
INSERT INTO TAB_TEST VALUES
('Ho','Kim', 7475393956, 'XYZ@HOTMAIL.COM')
/
INSERT INTO TAB_TEST VALUES
('Ho','Kim', 7475393956, 'XYZ@HOTMAIL.COM')
/
INSERT INTO TAB_TEST VALUES
('Ho','Kim', 7473430336, null)
/
INSERT INTO TAB_TEST VALUES
('Ho','Kim', 7195739945, 'XYZ@HOTMAIL.COM')
/
INSERT INTO TAB_TEST VALUES
('Ho','Kim', 7475393956, '123@HOTMAIL.COM')
/
INSERT INTO TAB_TEST VALUES
('Ho','Kim', null, '123@HOTMAIL.COM')
/
INSERT INTO TAB_TEST VALUES
('Ho','Kim', 1168548666, '456@HOTMAIL.COM')
/
INSERT INTO TAB_TEST VALUES
('Ho','Kim', 1168548666, '456@HOTMAIL.COM')
/
INSERT INTO TAB_TEST VALUES
('Ho','Kim', 1168548666, null)
/
INSERT INTO TAB_TEST VALUES
('Ho','Kim', 7473430336, null)
/
ALTER TABLE TAB_TEST ADD ID NUMBER
/

要更新的脚本

DECLARE 
lncount   NUMBER := 1; 

-- had to create this record in order to add the rowid
TYPE lttab_test IS RECORD( 
  ROWID VARCHAR2(200), 
  last_name VARCHAR2(200), 
  first_name VARCHAR2(200), 
  phone NUMBER, 
  email VARCHAR2(200), 
  id NUMBER); 

TYPE lttype 
  IS TABLE OF LTTAB_TEST; 

larecords LTTYPE; 
BEGIN 
SELECT ROWID, 
       last_name, 
       first_name, 
       phone, 
       email, 
       id 
bulk   collect INTO larecords 
FROM   tab_test a; 


FOR i IN 1..larecords.count LOOP 

    IF Larecords(i).id IS NULL THEN 
      FOR j IN 1..i-1 LOOP 
          IF Larecords(j).phone = Larecords(i).phone
             AND Larecords(i).id IS NULL THEN 
            Larecords(i).id := Larecords(j).id; 

            exit; 
          END IF; 
      END LOOP; 

      FOR j IN 1..i-1 LOOP 
          IF Larecords(j).email = Larecords(i).email 
             AND Larecords(i).id IS NULL THEN 
            Larecords(i).id := Larecords(j).id; 

            exit; 
          END IF; 
      END LOOP; 

      IF Larecords(i).id IS NULL THEN 
        Larecords(i).id := lncount; 

        lncount := lncount + 1; 
      END IF; 

    END IF; 
END LOOP; 

forall i IN 1..larecords.count 
  UPDATE tab_test 
  SET    id = Larecords(i).id 
  WHERE  ROWID = Larecords(i).ROWID; 

COMMIT:
END;