Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Cursor to increment value by 3 with each loop

Posted on 2006-11-17
4
Medium Priority
?
2,308 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 200 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 200 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

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

916 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