我正在尝试基于同一表(学生表)中的另一列和另一表(学校表)中的一列来更新列
代码为:
update student_table
set student_code =
(select l.student_code
from school_table l, student_table n
where l.school = n.schoolname)我得到以下错误
ORA - 01427单行子查询返回多行
任何帮助都将不胜感激。
发布于 2012-04-03 22:15:56
对你想要实现的目标有一个简单的英语解释会很有帮助。话虽如此,在我看来,假设school_table和student_table之间存在一对多关系,并将内部select作为与外部update语句相关的子查询,则您可以使用下面的SQL完成您想要做的事情:
update student_table
set student_code = (select l.student_code
from school_table
where school_table.school = student_table.schoolname)
;希望这能有所帮助。
问候你,罗杰
发布于 2012-04-02 11:24:20
如果运行您的子查询,您会发现它返回多个行。您正在尝试更新一个列,使其等同于您的子查询的结果,因此它只需要一个值。您应该将子查询限制为只返回一行,比如使用max()或min(),或者您打算连接到外部student_table?尝试:
update student_table n
set student_code =
(select l.student_code
from school_table l
where l.school = n.schoolname);发布于 2013-06-27 10:54:12
我们都确切地知道这个错误是怎么说的。set期望每列只设置一个值。我们想要实现的是使用另一个表的列中的值更新给定列的所有行。
下面是解决方案:
BEGIN
For i in (select col_X, col_Y from table1)
LOOP
Update table2 set col1 = i.col_X where col2 = i.col_Y;
END LOOP;
END;这正是您在SQLDeveloper worksheet上运行它的方式。他们说它很慢,但这是我在这个案例中唯一有效的解决方案。
https://stackoverflow.com/questions/9970480
复制相似问题