Solved

Cursor does not update last record

Posted on 2010-09-22
6
245 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

895 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

16 Experts available now in Live!

Get 1:1 Help Now