Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to update random rows?

973995Nov 13 2012 — edited Nov 13 2012
Hello All,

I have got answers table, there are two columns which belong to that table. This table consists 1 million rows. The answer column can be numeric or text.
create table answers(
answer_id number primary key,
answer varchar2(50)
);
My question is

I want to update 50 random rows to 100 which is equal to '0' (zero).

The below code works but I just wonder does it work correctly? I mean, I want to choose these rows randomly. However it is very hard to update random. Is this the best way? Do you have any suggestions?
update answers set answer = '100' where answer_id in( 
with
get_ids as (select answer_id from answers where answer = '0' and rownum <= 50 order by dbms_random.value)
select answer_id from get_ids);
In addition, when I read threads relevant to random update, somebody recommends using rowid, do you think should I use rowid instead of answer_id? (answer_id is also unique value in my table) If I use rowid will it be faster?

Thanks for your help.
This post has been answered by Frank Kulash on Nov 13 2012
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 11 2012
Added on Nov 13 2012
8 comments
1,952 views