简体   繁体   English

单行子查询返回多行

[英]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. 我编写了一个SQL查询来更新记录,在大多数情况下它运行良好,但是从昨天开始它无法更新行。我目前正在基于Spring MVC的Web应用程序上工作,在该应用程序中,我需要通过调用update执行DML操作()方法。

I am using JDBC template and in my update method i wrote this query. 我正在使用JDBC模板,并在我的更新方法中编写了此查询。 It fetches the 947 records for January month 2018 and I already checked all records are unique. 它获取了2018年1月的947条记录,我已经检查了所有记录是否唯一。

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 这是我的DAOImpl类中的Java代码

 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) SELECT更改为SELECT MAX(sd.emp_code)
  • Change the WHERE with AND rownum = 1 AND rownum = 1更改WHERE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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