Solved

Cursor does not update last record

Posted on 2010-09-22
6
249 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

828 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