Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Problem with cursor, I found out another way but I'd like to understand where I'm wrong

$
0
0

Hi everyone, a question...I have this table History2 for athlete

[NOME2] [varchar](50) NULL,
[BL2] [int] NULL,
[AM2] [int] NULL,
[SX2] [int] NULL,
[per] [nvarchar](5) NULL

Output

M.P.  7  8  11 null

A.R.  7  7  11 null

E.B.  8  9    6 null

..........

Bl2, Am2, Sx2 are filled with the last results of the prove, I added [per] cause I need to count and group by [per] to understand where the athletes are stronger ( there are more than 5000 athletes around the world). To do this I updated [per] whit a varchar,  

Output after update below

M.P.  7  8  11 Sx

A.R.  7  7  11 Sx

E.B.  8  9    6 Am

..........

To achieve this results ( I hate this editor because I don'understand what happens...) I used this code:

declare @imax as int
DECLARE @1 AS INT
set @imax=@@rowcount
set @1 = 1
WHILE (@1 <=@imax)
BEGIN
SELECT TOP 1 * FROM history2 WHERE per is not null
update history2 SET per=iif (bl2>am2 and bl2>sx2,'Bz',IIf(am2>sx2,'Am','Sx'))
SET @1 = @1 + 1 
END

It's works perfectly. I got the new column with the results I want.

I tried, anyway, with the cursor but it didn't works. 

declare productcursor cursor for
select * from HISTORY2
open productcursor
fetch next from productcursor 
while (@@FETCH_STATUS=0)
update history2 SET per=iif (bl2>am2 and bl2>sx2,'Bz',IIf(am2>sx2,'Am','Sx'))
fetch next from productcursor
close productcursor
deallocate productcursor   

I know I'm wrong in some way but...kindly ( Joe please stay away...), can someone explain where I'm wrong? I never used cursor before and I'm having some trouble to understand the logic.

Thank you a lot

...and Joe, please, stay away.  





Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>