Solved

Cursor does not update last record

Posted on 2010-09-22
6
250 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
[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
  • 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
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.

739 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