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
 General SQL Server Forums
 New to SQL Server Programming
 CURSORS !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Author  Topic 

rahulmalhotra26
Starting Member

23 Posts

Posted - 2008-02-03 : 23:09:36

declare @sal char(30),
@name char(30),
@sal2 char(30),
@i int
set @i=0

DECLARE Employee_Cursor4 CURSOR for
SELECT salary
FROM test2
OPEN Employee_Cursor4

fETCH FROM Employee_Cursor4 into @sal
while @@fetch_status=0

BEGIN

declare emp10 cursor for
select salary, fname from test2
open emp10
fetch from emp10 into @sal2, @name
PRINT 'this is NOT the desired output which i wanted from so long :'+@sal
while @@fetch_status=0 and @sal2=@sal
begin
print 'this is the name '+@name
fetch next from emp10 into @sal2,@name
end
close emp10
deallocate emp10

fetch next from employee_cursor4 into @sal
print @i
set @i=@i+1
end

close employee_cursor4
deallocate employee_cursor4

i HAVE CREATED A TEST TABLE WITH TWO COLUMNS SALARY AND FNAME
TABLE STRUCTURE IS LIKE THIS:

salary fname
10 r
20 m
30 n
40 p

when i run the above query it produces this result

this is NOT the desired output which i wanted from so long :10
this is the name r
0
this is NOT the desired output which i wanted from so long :20
1
this is NOT the desired output which i wanted from so long :30
2
this is NOT the desired output which i wanted from so long :40
3



i want it to say the name after every row:

this is NOT the desired output which i wanted from so long :10
this is the name r
0
this is NOT the desired output which i wanted from so
long :20
this is the name n 1

this is NOT the desired output which i wanted from so long :30
this is the name m
2

this is NOT the desired output which i wanted from so long :40
this is the name p
3


if any other way to do it please helpppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp


Rahul

binilmb
Starting Member

2 Posts

Posted - 2008-02-04 : 00:10:13
If u have a unique id in the table, u can do it using while loop, which is much faster than cursors....

Binil...
Go to Top of Page

rahulmalhotra26
Starting Member

23 Posts

Posted - 2008-02-04 : 00:31:18
well salary thing is the unique id.. i am not sure why it doesnt do the job

Rahul
Go to Top of Page

sunsanvin
Master Smack Fu Yak Hacker

1274 Posts

Posted - 2008-02-04 : 01:10:33
can some one write the while loop for the above cursor?(to replace that)

Vinod
Even you learn 1%, Learn it with 100% confidence.
Go to Top of Page

PeterNeo
Constraint Violating Yak Guru

357 Posts

Posted - 2008-02-04 : 02:04:45
add where clause to the stmt

declare emp10 cursor for
select salary, fname from @test2
WHERE salary = @sal -- add this

May i know why u used cursor
Go to Top of Page

rahulmalhotra26
Starting Member

23 Posts

Posted - 2008-02-04 : 03:56:20
peter i used a cursor because the department i work in they make reports on notepad .. they dont use reporting tools and and i have to get a report made wid print statements on analyzer,, i could have used while loop but this made my life easier wid cursors...

Rahul
Go to Top of Page

rahulmalhotra26
Starting Member

23 Posts

Posted - 2008-02-04 : 03:56:37
thanks for your reply peter

Rahul
Go to Top of Page
   

- Advertisement -