Solved

I need to update every twentieth row.

Posted on 2011-03-09
7
226 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 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:Huseyin KAHRAMAN
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
Independent Software Vendors: 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 51

Assisted Solution

by:Huseyin KAHRAMAN
Huseyin KAHRAMAN 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 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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Regarding Disk IO 3 46
Using datetime as triggers 2 22
Specify timing interval fro change data 2 56
What Is an Error? 2 26
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

749 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