0

I have an ORACLE query which does not work for this query.

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)
);

My Problem is I want to update mutiple records in the table emp (A) to status to 'PROGRESS'.But when executing this query all records in a.Enq_status is changes.Please help me in this.Updation is not correct.Please help me in this

1
  • What are the values in a and b when a wrong update occurs? Commented Aug 30, 2011 at 12:48

3 Answers 3

5

You have specified table emp both in the update query and the subquery too, Oracle will treat these as seperate tables. You need a correlated subquery:

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)); 

You could probably just update the emp table without the need for the subquery though if you link to the Data table in the where clause...

Sign up to request clarification or add additional context in comments.

3 Comments

@peter:What u said is correct.But I have tried the same.The updation works.But all rows in emp a are updated.To state it clearly all rows in emp a which does not match the conditon in where exist clause should remain unchanged.But they are affected.
I'd pull out the a-only where clause conditions so it looks like "where a.SA_End_Status is null and a.EQ_Status = 'ACTIVATED' and a.ActivationReturnCode = 0 and a.Alert_Code > 0 and exists (select 'x' from Data b where a.User_Id = b.User_Id... Should not affect the results, but it would be cleaner.
@Jim, just done that in a new answer, it makes it a bit clearer what the OP is then trying to do.
3

Your update statement will update all the records in emp table because you don't specify the records to update. If your sub-query returns at least one row, the all the emp records will be updated. If it returns no rows, then none of the records will be updated.

Change your update like this:

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 Comments

@peter:where id in(select)..Why we are using the id field...I dont have such a field...Tell me the use of it,so that i could understand
I assumed that you have an id field. The idea is to use the primary key of the emp table. Change in the query, instead of id, put your primary key.
3

Try:

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.