Monday, July 30, 2012

Updating every Nth Row in TSQL

This is another I know I've done this before but can't remember the syntax thing.

 update tableone
 set fieldex = 'valueOne'
 where clsid % 3 = 0
 and fieldex is null

For every ID that's divisible by 3 with no remainder, it updates.

Theoretically, you could pass in the n (3, in this case), and put in some more programming and loop it. But that's more complex code that I might put in the front end, or an admin console, and not in TSQL (personal preference).

No comments:

Post a Comment