Solved

Cursor does not update last record

Posted on 2010-09-22
6
243 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
how to import 7 54
Passing value to a stored procedure 8 68
SQl Server - background task to delete records. 8 22
Sql query 34 20
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

758 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

20 Experts available now in Live!

Get 1:1 Help Now