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 |
|
rahulmalhotra26
Starting Member
23 Posts |
Posted - 2008-02-03 : 23:09:36
|
   declare @sal char(30),@name char(30), @sal2 char(30),@i intset @i=0DECLARE Employee_Cursor4 CURSOR forSELECT salary FROM test2OPEN Employee_Cursor4fETCH 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 emp10fetch next from employee_cursor4 into @sal print @iset @i=@i+1endclose employee_cursor4deallocate employee_cursor4i HAVE CREATED A TEST TABLE WITH TWO COLUMNS SALARY AND FNAME TABLE STRUCTURE IS LIKE THIS:salary fname10 r20 m 30 n40 pwhen i run the above query it produces this resultthis is NOT the desired output which i wanted from so long :10 this is the name r 0this is NOT the desired output which i wanted from so long :20 1this is NOT the desired output which i wanted from so long :30 2this is NOT the desired output which i wanted from so long :40 3i 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 0this is NOT the desired output which i wanted from so long :20this is the name n 1this is NOT the desired output which i wanted from so long :30 this is the name m 2this is NOT the desired output which i wanted from so long :40 this is the name p3 if any other way to do it please helppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppRahul |
|
|
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... |
 |
|
|
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 jobRahul |
 |
|
|
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)VinodEven you learn 1%, Learn it with 100% confidence. |
 |
|
|
PeterNeo
Constraint Violating Yak Guru
357 Posts |
Posted - 2008-02-04 : 02:04:45
|
| add where clause to the stmtdeclare emp10 cursor forselect salary, fname from @test2WHERE salary = @sal -- add this May i know why u used cursor |
 |
|
|
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 |
 |
|
|
rahulmalhotra26
Starting Member
23 Posts |
Posted - 2008-02-04 : 03:56:37
|
| thanks for your reply peterRahul |
 |
|
|
|
|
|
|
|