0

我有两个表,我需要使用基于公共列的第一个表中的值更新第二个表。

我使用了以下语句

UPDATE emp
  2      SET ename = ( SELECT dname
  3                    FROM dept
  4                    WHERE emp.deptno = dept.deptno)
  5      WHERE EXISTS
  6        ( SELECT dname
  7                    FROM dept
  8                    WHERE emp.deptno = dept.deptno);

但我得到了错误

ORA-01427 - Single row subquery returns more than one row.

你能帮帮我吗?

4

3 回答 3

0
SELECT dname
FROM dept
WHERE emp.deptno = dept.deptno

查询必须返回单个记录。

检查

SELECT count(*), dname
FROM dept
group by dname
having count(*) > 1
order by 1 desc

或使用

SELECT dname
FROM dept
WHERE emp.deptno = dept.deptno
and rownum = 1
于 2013-01-11T07:22:09.763 回答
0

您需要检查第一个子查询是否仅返回 1 个值 bcz 如果此子查询返回多于 1 行您想用行中的 2 个值更新 1 个字段名称,这就是为什么我猜您有此错误

于 2013-01-11T07:22:17.547 回答
0

Your subquery to dept table is probably not returning one row. Add min or max operation to get one row from dept for each row from emp. Each setting value must be nonambigous.

UPDATE emp
    SET ename = ( SELECT min dname
                  FROM dept
                  WHERE emp.deptno = dept.deptno)
    WHERE EXISTS
      ( SELECT dname
                  FROM dept
                  WHERE emp.deptno = dept.deptno);

Remark1: in default SCOTT schema deptno is primary key, so no error would be encountered.
Remark2: shouldn't your question to be placed in serverfault.com?

于 2013-01-11T07:28:22.827 回答