Friday, March 31, 2006

(SQL) How do I update only the top # rows from a table?

This can be specially handy when you are testing your data, but it can apply to many production cases as well, I've seen people invent so many ways to do this, but here's a simple way of doing it

set rowcount 100
update YourTable set SomeField = 'Something' where SomeField is null
set rowcount 0

it is very important that you run set rowcount 0 after finishing whatever you needed rowcount for, else any other query (select, updates, etc) that you run will only run for the first # records and it can really give you some headaches, so whenever you write

set rowcount #
write
set rowcount 0

immediatly after that, then write the query in the middle that requires to be ran with the limited rowcount

No comments: