1 reply
Is do I really really really need to use a cursor?
They are sadly inefficient and avoiding using them is a good thing.
Whether you execute one inside a sql batch or an SP, makes no difference in terms of syntax and semantics.
As for you're request to clarify, well I need some clarification.
A cursor is effectively a query, with a pointer to a record in the result set.
Perhaps this naff example of something that should not be done with a cursor will help. It's in T-SQL, seeing as you didn't mention which DBMS you were using.
Declare @ID int
Declare @Name VarChar(50)
Declare MyCursor Cursor For Select MyID,MyName From MyTable
Open MyCursor
Fetch First MyCursor Into @ID,@Name
While @@Fetch_Status = 0
Begin
If @ID < 1000 and @Name = 'Unknown'
Update MyTable Set MyName = 'X' + Convert(VarChar(49),@ID) Where MyID = @ID
Fetch Next MyCursor Into @ID, @Name
End
Close MyCursor
DeAllocate MyCursor
Put the above inside an SP, (I didn't check for syntax errors), and given MyTable existsa and has the two columns it will work.
The close and deallocate are very important, if you make the classic mistake of forgetting them , you get failures the next time you try to declare it in the same session.
HtHs
This discussion is closed to new replies.
Trending Web Development posts
Sort by Trending