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.
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 moneydeclare @buynow moneyset @total=0declare my_cursor cursorforselect buynow from items where status='buynow' and buyer_id=23open my_cursorfetch next from my_cursor into @buynowWHILE (@@fetch_status = 0)BEGIN set @total=@total+@buynow fetch next from my_cursor into @buynowENDclose my_cursordeallocate my_cursorprint 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? |
 |
|
nicromonicon
Starting Member
8 Posts |
Posted - 2008-05-04 : 01:13:51
|
its a project,and its driving me crazy! pleae help! |
 |
|
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 itemswhere status='buynow' and buyer_id=23 also make sure you specify a length when casting to varcharprint cast(@total as varchar(length)) |
 |
|
nicromonicon
Starting Member
8 Posts |
Posted - 2008-05-04 : 09:31:44
|
the length thing worked !!thanks visakh :) |
 |
|
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 |
 |
|
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 totalfrom itemswhere status='buynow' and buyer_id=23An infinite universe is the ultimate cartesian product. |
 |
|
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 totalfrom itemswhere status='buynow' and buyer_id=23An 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. |
 |
|
|
|
|