Getting error during update SQL query single-row subquery returns more than one row

时间:2019-05-31 11:58:33

标签: oracle oracle11g oracle10g oracle-sqldeveloper

I have 2 tables "HOF" and "WE_GROUP_HOF_K"

In "HOF" table total "2066666" records

In "WE_GROUP_HOF_K" table total "116" records

I have SQL query who find duplicate cnicno column with hofid column from 2 tables "HOF" and "WE_GROUP_HOF_K"

This query found total "120" duplicate cnicno records

QUERY:

SELECT hofid, cnicno
     FROM hof 
    WHERE cnicno IN (SELECT cnic_no FROM we_group_hof_k);

In "WE_GROUP_HOF_K" table there are 1 empty column "GROUP_HOF_ID". Now I want to update duplicate records of "HOFID" column into "GROUP_HOF_ID" column

I am getting this error while update records:

ORA-01427: single-row subquery returns more than one row

Update SQL Query:

UPDATE we_group_hof_k wg
 SET wg.GROUP_HOF_ID =
      (SELECT h.hofid
         FROM hof h
        WHERE h.cnicno = wg.cnic_no);

1 个答案:

答案 0 :(得分:0)

如果所有CNICNO共享相同的HOFID,则使用DISTINCT

UPDATE we_group_hof_k wg
 SET wg.GROUP_HOF_ID =
      (SELECT DISTINCT h.hofid           --> here
         FROM hof h
        WHERE h.cnicno = wg.cnic_no);

如果没有,您想选哪个HOFIDMINMAX也会有所帮助,例如

UPDATE we_group_hof_k wg
 SET wg.GROUP_HOF_ID =
      (SELECT MIN(h.hofid)               --> here
         FROM hof h
        WHERE h.cnicno = wg.cnic_no);
相关问题