Skip to Main Content

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Mohammed.

Asked: March 03, 2011 - 1:28 am UTC

Last updated: March 03, 2011 - 9:47 am UTC

Version: 10.2

Viewed 10K+ times! This question is

You Asked

I've a table containing site_id and staff_id and is_valid columns . I would like to set is_valid as 'N' if site_id , staff_id combination is not unique. I used the following query to achieve this, but table is getting locked as I'm updating the same table from which I'm selecting the records. Please suggest a workaround for this.

UPDATE table1 a
   SET is_valid = 'N'
 WHERE NOT EXISTS (SELECT   1
                       FROM table1 b
                      WHERE b.site_id = a.site_id
                   GROUP BY site_id, staff_id
                     HAVING COUNT (1) = 1);


and Tom said...

Um, no, the table is not getting locked up - you cannot be locking yourself out - no chance with that statement.

merge into table1 a
using (select site_id, staff_id
         from table1
        group by site_id, staff_id
       having count(*) > 1) b
on (a.site_id = b.site_id and a.staff_id = b.staff_id)
when matched then update set is_valid = 'N';



I wrote this from your specification:

s . I would like to set is_valid as 'N' if site_id , staff_id combination is not unique.

NOT from your example (it is missing a join on staff_id as far as I can tell)

Is this answer out of date? If it is, please let us know via a Comment