Solved

I need to update every twentieth row.

Posted on 2011-03-09
7
220 Views
Last Modified: 2012-05-11
I need to update every twentieth row.

Is there a way in which I can do this without cursors?
0
Comment
Question by:Mr_Shaw
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 300 total points
ID: 35084427
you need some "order by" to specify which row is the 20th ...
;with data as (
  select t.*, row_number() over (order by somekeycol) rn
    from yourtable t
)
UPDATE data
    SET x = abc
  WHERE rn % 20 = 0 

Open in new window

0
 
LVL 15

Accepted Solution

by:
derekkromm earned 100 total points
ID: 35084435
update t
set ?=?
from table t
inner join (select PK, row_number() over (order by PK) as 'rownum' from table) t1
on t.PK = t1.PK
where (t1.rownum-1) % 20 = 0
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 35084562
or this:

update t
set myCol=@value
from table t
inner join (
  select PK from (
    select PK, row_number() over (order by PK) as rn from table
  ) x where rn % 20 =0
) t1
on t.PK = t1.PK
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

Author Comment

by:Mr_Shaw
ID: 35084630
angelIII:

Hi Would, this be a random 1 in 20 or last 20% of rows?
0
 
LVL 51

Assisted Solution

by:HainKurt
HainKurt earned 100 total points
ID: 35084908
try:

update t
set myCol=@value
from table t
inner join (
  select PK from (
    select PK, row_number() over (order by newid()) as rn from mytable
  ) x where rn % 20 =0
) t1
on t.PK = t1.PK
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35084923
% is the modulo operator, nothing about random or last 20 rows ...
http://msdn.microsoft.com/en-us/library/aa276866%28v=sql.80%29.aspx
0
 

Author Closing Comment

by:Mr_Shaw
ID: 35085255
thanks
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now