Solved

Cursor does not update last record

Posted on 2010-09-22
6
248 Views
Last Modified: 2012-05-10
I have the attached SQL script. I am selecting an initial recordset of 100 records and looping through each to update accordingly but it appears that the update is only applied to the first 99 records (the 100th record is not being update)...am I missing something?
declare
		@RecordID int,
		@Price int

declare csr cursor for

	Select top 100 a.RecordID
	FROM Records
	ORDER BY
		a.RecordDate desc

	open csr

	fetch next from csr into
			@RecordID

	while @@FETCH_STATUS = 0
		begin  --cursor loop

			set @Price = (SELECT Price
							FROM Records 
			where RecordID = @RecordID)

			if @Price > 0
				begin
					update Records
					set Priced = 1
					where RecordID = @RecordID
				end
			else
				begin
					update Records
					set Priced = 0
					where RecordID = @RecordID
			end

			fetch next from csr into
					@RecordID
		end  --cursor loop

	close csr
	deallocate csr

Open in new window

0
Comment
Question by:felkamau
  • 4
6 Comments
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33738760
;WITH top100 as (
      Select top 100 a.RecordID
      FROM Records
      ORDER BY a.RecordDate desc)
UPDATE Records
Set Priced= case when Price>0 then 1 else 0 end
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33738765
Sorry! Update top 100 only

;WITH top100 as (
      Select top 100 a.RecordID
      FROM Records
      ORDER BY a.RecordDate desc)
UPDATE top100
Set Priced= case when Price>0 then 1 else 0 end
0
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 total points
ID: 33738788
Re why the cursor is updating only 99

(1) do you have duplicate RecordIDs? top 100 could include a pair of duplicates
(2) the cursor may have to be declared static to get a static copy of the first 100 records

declare csr cursor STATIC for
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 3

Expert Comment

by:_bmendoza
ID: 33738891
I see no problem except for "a" alias you'll get

The multi-part identifier "a.RecordID" could not be bound.
The multi-part identifier "a.RecordDate desc" could not be bound.

why not  just do the following?

alter table Records
drop column Priced

alter table Records
add Priced as case when price > 0 then 1else 0 end

or there's something else as a reason for using cursor.
0
 

Author Comment

by:felkamau
ID: 33738983
declare STATIC works.
Thank You!
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33739003
Just FYI, but if the cursor does all that it shown above (and only that), you only need the update query... which will be only 10-100 times faster.
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

813 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

18 Experts available now in Live!

Get 1:1 Help Now