简体   繁体   中英

Single Row Sub query return more than one row

I wrote a SQL query which updates the record, in most cases it runs fine, but from yesterday it fails to update the row.I am currently working on Spring MVC based Web application, in which I need to perform the DML operation by calling update()method.

I am using JDBC template and in my update method i wrote this query. It fetches the 947 records for January month 2018 and I already checked all records are unique.

I am here stuck, i am not able to find the duplication of record.I thought this exception occurred because of multiple record, but I think i am wrong, there is something which i am not able to identify.

Here is the query:

    UPDATE SALARY_DETAIL_REPORT_012018 sd         
SET sd.EMP_ID = 
(SELECT e.emp_id from EMPLOYEE e 
WHERE e.VERIFY_STATUS = 1 
AND e.RELIEF_TYPE IS NULL                                   
AND e.emp_code = to_char(sd.emp_code) 
AND e.EMP_TYPE_ID!=03)                                   
WHERE EXISTS 
(SELECT e.emp_id from EMPLOYEE e 
WHERE e.VERIFY_STATUS = 1                           
AND e.emp_code = to_char(sd.emp_code) 
AND e.EMP_TYPE_ID!=03 AND e.RELIEF_TYPE IS NULL)           
AND sd.EMP_ID IS NULL      
AND sd.REFERENCE_ID LIKE '203-%'

and HERE is Java Code in my DAOImpl class

 public void update(String tableSuffix, String regionId, String circleId) {
        String tabName = "SALARY_DETAIL_REPORT_" + tableSuffix;
            String q = "UPDATE " + tabName + " sd"
                    + "         SET sd.EMP_ID = (SELECT e.emp_id "
                    + "                                  from EMPLOYEE e WHERE e.VERIFY_STATUS = 1 AND e.RELIEF_TYPE IS NULL "
                    + "                                  AND e.emp_code = to_char(sd.emp_code) AND e.EMP_TYPE_ID!=03) "
                    + "                                  WHERE "
                    + "         EXISTS (SELECT e.emp_id from EMPLOYEE e WHERE e.VERIFY_STATUS = 1 "
                    + "                          AND e.emp_code = to_char(sd.emp_code) AND e.EMP_TYPE_ID!=03 AND e.RELIEF_TYPE IS NULL) "
                    + "          AND sd.EMP_ID IS NULL";
            if (circleId != null && !circleId.trim().equals("")) {
                q += "      AND sd.REFERENCE_ID LIKE '" + circleId + "-%' ";
            } else {
                q += "      AND sd.REFERENCE_ID LIKE '" + regionId + "-%' ";
            }

            //   System.out.println("q " + q);
            MapSqlParameterSource param = new MapSqlParameterSource();
            getNamedParameterJdbcTemplate().update(q, param);

    }

Please suggest me the best solution

You need to find the rows that prevent your query from running.

Run this query:

SELECT sd.emp_code, COUNT(*) as cnt 
FROM EMPLOYEE e 
WHERE e.VERIFY_STATUS = 1 AND
      e.RELIEF_TYPE IS NULL  AND                                 
      e.emp_code = to_char(sd.emp_code) AND
      e.EMP_TYPE_ID <> 03
GROUP BY sd.emp_code
HAVING COUNT(*) > 1;

This has the candidate problems. What can you do? The simplest thing is one of the problems:

  • Change the SELECT to SELECT MAX(sd.emp_code)
  • Change the WHERE with AND rownum = 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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