Oracle更新查询不起作用

时间:2011-08-30 12:44:03

标签: oracle oracle11g

我有一个ORACLE查询,不适用于此查询。

UPDATE emp a set a.Enq_Status = 'PROGRESS' 
where exists (select * from emp  a, Data b   
         WHERE a.SA_Enq_Status is NULL 
           and a.EQ_Status = 'ACTIVATED' 
           and a.Activation_Return_Code = 0 
           and a.Alert_Code > 0 
           and a.User_Id = b.User_Id 
           and (b.Is_Associate is NULL or b.Is_Associate = 0)  
           and (b.Stk_Schd is NULL)
           and (b.Stk_Dis_Amt is NULL)
);

我的问题是我想将表emp(A)中的多个记录更新为'PROGRESS'的状态。但是当执行此查询时,a.Enq_status中的所有记录都是更改。请帮我解决这个问题。使用不正确请帮我这个

3 个答案:

答案 0 :(得分:5)

您在更新查询和子查询中都指定了表emp,Oracle会将这些视为单独的表。 您需要一个相关的子查询:

UPDATE emp a
   set a.Enq_Status = 'PROGRESS'   
 where exists (select 'X'                   
                 from Data b                   
                WHERE a.SA_Enq_Status is NULL                    
                  and a.EQ_Status = 'ACTIVATED'                    
                  and a.Activation_Return_Code = 0                    
                  and a.Alert_Code > 0                    
                  and a.User_Id = b.User_Id                    
                  and (b.Is_Associate is NULL or b.Is_Associate = 0) 
                  and (b.Stk_Schd is NULL)
                  and (b.Stk_Dis_Amt is NULL)); 

如果你链接到where子句中的Data表,你可能只需更新emp表而不需要子查询...

答案 1 :(得分:3)

您的更新语句将更新emp表中的所有记录,因为您未指定要更新的记录。如果您的子查询返回至少一行,则将更新所有emp记录。如果没有返回任何行,则不会更新任何记录。

更改您的更新:

UPDATE emp SET Enq_Status = 'PROGRESS' 
WHERE id in 
(SELECT a.id 
 FROM emp  a, Data b  
 WHERE a.SA_Enq_Status is NULL and a.EQ_Status = 'ACTIVATED' 
   and a.Activation_Return_Code = 0 and a.Alert_Code > 0 
   and a.User_Id = b.User_Id and (b.Is_Associate is NULL or b.Is_Associate = 0)
   and (b.Stk_Schd is NULL)and (b.Stk_Dis_Amt is NULL)
);

答案 2 :(得分:3)

尝试:

UPDATE emp a
   SET a.enq_status = 'PROGRESS'
 WHERE a.sa_enq_status IS NULL
   AND a.eq_status = 'ACTIVATED'
   AND a.activation_return_code = 0
   AND a.alert_code > 0
   AND EXISTS
       (SELECT 'X'
          FROM data b
         WHERE a.user_id = b.user_id
           AND ( b.is_associate IS NULL OR b.is_associate = 0 )
           AND b.stk_schd IS NULL
           AND b.stk_dis_amt IS NULL);