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 : 20:25:29
declare @sal char(50)

DECLARE Employee_Cursor1 CURSOR static for
SELECT salary
FROM test2


OPEN Employee_Cursor1

FETCH FROM Employee_Cursor1 into @sal


WHILE @@FETCH_STATUS = 0

BEGIN
FETCH NEXT FROM Employee_Cursor1 into @sal
PRINT 'this is NOT the desired output which i wanted from so long :'+@sal

END

CLOSE Employee_Cursor1
DEALLOCATE Employee_Cursor1

Rahul

rahulmalhotra26
Starting Member

23 Posts

Posted - 2008-02-03 : 20:26:01
THIS TABLE GIVES ME THIS OUTPUT
20
30
40
40

Rahul
Go to Top of Page

rahulmalhotra26
Starting Member

23 Posts

Posted - 2008-02-03 : 20:27:11
THE TABLE I MADE TO TEST THIS IS HAVING ONLY ONE COLUMN SALARY WITH FOLLOWING ROWS
10
20
30
40
.. HOW CAN I GET THE OUTPUT AS 20,30,40

AND WHY DOES IT REPEAT 40 TWO TIMES..

Rahul
Go to Top of Page

binilmb
Starting Member

2 Posts

Posted - 2008-02-04 : 00:17:47
Before the while loop ur fetching the record value to the variable...
and also in the loop again ur fetching the record value before printing the fetched value....
So give the fetch in the while loop after printing the first value... just like i mentioned below


declare @sal char(50)

DECLARE Employee_Cursor1 CURSOR static for
SELECT salary
FROM test2


OPEN Employee_Cursor1

FETCH FROM Employee_Cursor1 into @sal


WHILE @@FETCH_STATUS = 0

BEGIN
PRINT 'this is NOT the desired output which i wanted from so long :'+@sal
FETCH NEXT FROM Employee_Cursor1 into @sal
END

CLOSE Employee_Cursor1
DEALLOCATE Employee_Cursor1

Binil...
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-02-04 : 01:30:42
Declare @salary varchar(8000)
select @salary=isnull(@salary+',','') from test2
select @salary

But better you do this concatenation at fron end application


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-02-04 : 01:42:51
Duplicate post
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=96691



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page
   

- Advertisement -