Friday, April 3, 2009

Update a particular number of rows

There are times when you only want to update a specific number of rows. In Oracle, the way to do this is to use the rownum variable in the following way:

Update x
set x.foo = 'hello'
where rownum <= 500;

This way, Oracle will only update the first 500 records it finds.

You can use rownum in virtually any DML statement. Try it on a select statement:

select x.foo
from x
where rownum <= 10;

Your result set will only have the first 10 records displayed on the screen.

No comments: