?
Solved

I need to update every twentieth row.

Posted on 2011-03-09
7
Medium Priority
?
228 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 1200 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 400 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 57

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Mr_Shaw
ID: 35084630
angelIII:

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

Assisted Solution

by:HainKurt
HainKurt earned 400 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 143

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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Suggested Courses

801 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