Solved

Cursor to increment value by 3 with each loop

Posted on 2006-11-17
4
2,126 Views
Last Modified: 2012-05-05
I'm using the following simple cursor in SQL Server 2005 and
I want it to add 3 to each value that get's inserted with each loop:

so if I start the first value of the balance at 1000 - the second time
through the loop - I want the balance to be 1003
the next time through I want the balance to be 1006

What happens with this cursor is it loops thourgh and makes all the balance rows equal to 1003


declare @balance varchar(15)
declare loopcurs cursor for
  SELECT balance +3 from acct_data
  for read only
open loopcurs
fetch next from loopcurs into @balance
while (@@fetch_status = 0)
begin
  Update acct_data
  set balance = @balance  
  where balance = '1000'
  fetch next from loopcurs into @balance
end /* while (@@fetch_status = 0) */
close loopcurs
deallocate loopcurs
0
Comment
Question by:kpu8
4 Comments
 
LVL 17

Expert Comment

by:HuyBD
ID: 17970055
kpu8, why dont you try one query without cursor

update acct_data set balance=balance +3
where balance in (1000,1003)
0
 
LVL 29

Accepted Solution

by:
Gautham Janardhan earned 50 total points
ID: 17970359
why dont u try this

DECLARE @Y INT
SElect @Y = balance from tablebal

UPDATE tablebal SET @Y = balance =  (@Y + 3)
0
 
LVL 28

Assisted Solution

by:imran_fast
imran_fast earned 50 total points
ID: 17970805
-- Hi,
-- this will mess up the data so you should better use the primary key of the table in the cursor
declare @PK_COL varchar(15), @Balance varchar(15),
      @Bal int
declare loopcurs cursor for
  SELECT primarykey, Balance  from acct_data
  for read only
open loopcurs
fetch next from loopcurs into @PK_COL,@balance
set @Bal = @Balance
while (@@fetch_status = 0)
begin
  set @Bal = @bal + 3
  Update acct_data
  set balance = @balance  
  where balance = @Balance  and Primarykeycolumn = @Pk_col
  fetch next from loopcurs into @PK_COL,@balance
end /* while (@@fetch_status = 0) */
close loopcurs
deallocate loopcurs
0
 
LVL 1

Author Comment

by:kpu8
ID: 17973085
Both worked thanks!

0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

743 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

12 Experts available now in Live!

Get 1:1 Help Now