用于更新列的SQL查询

时间:2011-07-15 07:14:56

标签: sql oracle

我有这个查询抛出错误-SQL给出错误

  

ORA-01427单行子查询返回多行

在Oracle 8中

UPDATE STG1_HFHS_ENR_EMP_ROSTER_2005 A
SET 
A.BUSINESS_UNIT =
( SELECT t.BUSINESS_UNIT
FROM STG1_HFHS_ENR_EMP_ROS_2005_TMP t, STG1_HFHS_ENR_EMP_ROSTER_2005 s
WHERE t.ID = s.ID ) 

请你帮我纠正上述错误。我必须更新一列"A.BUSINESS_UNIT =

2 个答案:

答案 0 :(得分:2)

试试这个:

UPDATE STG1_HFHS_ENR_EMP_ROSTER_2005 A
SET 
A.BUSINESS_UNIT =
( SELECT t.BUSINESS_UNIT
FROM STG1_HFHS_ENR_EMP_ROS_2005_TMP t
WHERE t.ID = A.ID AND ROWNUM < 2) 

答案 1 :(得分:0)

UPDATE stg1_hfhs_enr_emp_roster_2005 a
SET 
a.business_unit =
(SELECT business_unit
 FROM (SELECT business_unit
       FROM stg1_hfhs_enr_emp_ros_2005_tmp 
       ORDER BY ... -- some clause that puts the most relevant business_unit as first
      ) 
 WHERE id = a.id
   AND ROWNUM < 2);

不幸的是我没有访问Oracle 8来测试它。 ORDER和限制rowcount需要两层内联视图。此外,您不能直接在最深的内联视图中引用表“a”(一些愚蠢的Oracle限制),因此在中间视图中需要它。