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
 How to disable output of crusors

Author  Topic 

ozSQLServer
Starting Member

32 Posts

Posted - 2011-11-16 : 19:54:11
Hi all,

I've got a code that simple loop through rows in a table using cursors. I've noticed for each returned row, it's being displayed in SMSS.

Is there a way to avoid displaying every row returned from the cursor?


begin
SET XACT_ABORT ON
set nocount on
DECLARE curemployee CURSOR FOR SELECT BusinessEntityID FROM HumanResources.Employee2
BEGIN TRANSaction
OPEN curemployee
FETCH FROM curemployee

WHILE @@FETCH_STATUS = 0
BEGIN
FETCH FROM curemployee
END
CLOSE curemployee
DEALLOCATE curemployee
end
GO


Cheers,
ozSQL

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-11-16 : 20:27:21
You need to put the data into variables:

DECLARE @s varchar(50)
...

FETCH FROM curemployee INTO @s

...

FETCH FROM curemployee INTO @s


Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

ozSQLServer
Starting Member

32 Posts

Posted - 2011-11-16 : 23:47:53
Got it.. thanks a lot!

Cheers,
ozSQL
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-11-17 : 13:27:53
You're welcome, glad to help.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-11-17 : 13:33:57
But WHY are you using a cursor?

Cursors in SQL Server are very bad

99.99% of the time you don't need one



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-11-17 : 13:35:27
See...another SQL Server...ahem..."feature"

I never knew you could fetch from a cursor...INTO Nothing



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-11-17 : 13:35:54
Sure saves a lot on compiler code

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page
   

- Advertisement -