views:

49

answers:

2

I am working with cursors and successfully executed in T-SQL and the database I use is Microsoft SQL Server 2005.

My query is after I execute the cursor, the output is shown in message area. When I deallocate the cursor using deallocate <cursor name> it gets deallocated. Again I execute my cursor. Now in the message area I get this:

"Command(s) successfully completed "

and I am not getting the output of the cursor. To resolve, I copied the cursor code and opened a new query area and pasted there. It works perfect for the first time. When I rerun the cursor, the same message is displayed in the message area.

How do I re-execute the same cursor code in the same window rather than creating a new SQL window?

+1  A: 

Without seeing your code it is hard to say what is wrong, but I do know that cursors are code of last resort and can usually be done with less code and faster performance by using another method. See this link for ideas of better ways to handle your task:

http://wiki.lessthandot.com/index.php/Cursors_and_How_to_Avoid_Them

HLGEM
here is my cursor code my database name is exercise having a table employee with fields <empid*(int),ename(varchar(20)),sal(money),desigid^(int)> declare cur cursor for select eid,ename,sal from employeeopen cur while @@fetch_status=0 begin declare @empid int declare @empname varchar(20) declare @empsal money fetch next from cur into @empid,@empname,@empsal select @empid,@empname,@empsal endclose curdeallocate cur
vmani007
+1  A: 

You are changing state with your execution. What that means, I cannot tell w/o actually seeing your code. But your database and session are in one state when you execute the code first time, and different state when you execute it again. It could be something as simple as unbalanced transactions. The second time, in the new state, the cursor doesn't select anything perhaps, or your code takes a completely different IF branch and doesn't execute any cursor. W/o code, is impossible to tell exactly what is the difference in the database/session state.

Remus Rusanu
here is my cursor code my database name is exercise having a table employee with fields <empid*(int),ename(varchar(20)),sal(money),desigid^(int)> declare cur cursor for select eid,ename,sal from employeeopen cur while @@fetch_status=0 begin declare @empid int declare @empname varchar(20) declare @empsal money fetch next from cur into @empid,@empname,@empsal select @empid,@empname,@empsal endclose curdeallocate cur
vmani007
@Remus Rusanu : is there any way to view/see the state change after executing the cursor in MS SQL server 2005 ?
vmani007