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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Cursor

Author  Topic 

tedew
Starting Member

22 Posts

Posted - 2005-11-09 : 15:39:25

hello:)

I have a problem with building cursor:(
I have a table : tblData
and there are 3 records

I want to list them trought cursor
this is my code
--------------------------------------

DECLARE @data varchar(50)
declare C1 cursor for
Select id from tblWykladowca
open C1
FETCH NEXT FROM C1 into @data



while @@FETCH_STATUS =0
begin
SELECT @data
end

close C1
DEALLOCATE C1

but this cursor works,works,works .... almost 3000 recordc and still works and there are only 3 records in the table .
????

bye,
sorry for my english

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2005-11-09 : 15:50:44
why do you want to use a cursor? why not just use a SELECT?
select id from tblWykladowca

the problem with your cursor is you didn't put another "fetch next" inside the while loop.

Be One with the Optimizer
TG
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-11-10 : 00:31:20
What is the purpose of using Cursor in this case?

Refer this to know how to avoid cursors in many cases
http://www.sql-server-performance.com/cursors.asp

Madhivanan

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

tedew
Starting Member

22 Posts

Posted - 2005-11-10 : 03:36:33
hello:)
I know that I can use "Select" to list all records in my table :) this is very simple cursor - I'm learning now this useful "function" in T-SQL :)

@TG
thanks for your help - it's working now :)
bye


Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2005-11-10 : 07:18:07
Where did you get the idea that cursors are useful?

Do yourself a favor, avoid cursors and learn SQL, you'll get a lot more out of it.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-11-10 : 07:21:10
>>Where did you get the idea that cursors are useful?

May be he is trying to simulate Client side For or While loops in SQL

Madhivanan

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

bridge
Yak Posting Veteran

93 Posts

Posted - 2005-11-10 : 08:11:24
Try after replacing this code with yours

while @@FETCH_STATUS =0
begin
SELECT @data
FETCH NEXT FROM C1 into @data
end
Go to Top of Page
   

- Advertisement -