Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 problem with cursor

Author  Topic 

nicromonicon
Starting Member

8 Posts

Posted - 2008-05-03 : 17:41:12
hey guys,i have this cursor which will return sum of column 'buynow' for a specific buyer,but it always returns 0..what am i doing wrong?
note..i have to use cursors!

your help is much appreciated
declare @total money
declare @buynow money
set @total=0
declare my_cursor cursor
for
select buynow
from items
where status='buynow' and buyer_id=23
open my_cursor
fetch next from my_cursor into @buynow

WHILE (@@fetch_status = 0)
BEGIN
set @total=@total+@buynow
fetch next from my_cursor into @buynow
END
close my_cursor
deallocate my_cursor
print cast(@total as varchar)

rmiao
Master Smack Fu Yak Hacker

7266 Posts

Posted - 2008-05-03 : 21:12:53
Why have to use cursor for this one?
Go to Top of Page

nicromonicon
Starting Member

8 Posts

Posted - 2008-05-04 : 01:13:51
its a project,and its driving me crazy! pleae help!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-05-04 : 03:07:02
quote:
Originally posted by nicromonicon

its a project,and its driving me crazy! pleae help!


Have you checked the result of this?

select buynow 
from items
where status='buynow' and buyer_id=23



also make sure you specify a length when casting to varchar

print cast(@total as varchar(length))
Go to Top of Page

nicromonicon
Starting Member

8 Posts

Posted - 2008-05-04 : 09:31:44
the length thing worked !!
thanks visakh :)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-05-04 : 10:01:53
quote:
Originally posted by nicromonicon

the length thing worked !!
thanks visakh :)


You're welcome
Go to Top of Page

cat_jesus
Aged Yak Warrior

547 Posts

Posted - 2008-05-05 : 11:50:46
Visakh16 did you mean to write this?

select sum(buynow) as total
from items
where status='buynow' and buyer_id=23




An infinite universe is the ultimate cartesian product.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-05-05 : 12:00:17
quote:
Originally posted by cat_jesus

Visakh16 did you mean to write this?

select sum(buynow) as total
from items
where status='buynow' and buyer_id=23




An infinite universe is the ultimate cartesian product.


Nope. i was asking him to check how whether he has has enough records with non zero values for buynow field for condition specified.
Go to Top of Page
   

- Advertisement -