SQL Server Cursors Question:
Explain the steps to use Transact-SQL Cursor?
Answer:
Declare the cursor,
Open the cursor,
Fetch record row by row,
Close cursor,
Deallocate cursor.
Example of a cursor
Declare @EmpId int
Declare curEmp CURSOR READ_ONLY FOR SELECT EmpId FROM Employee
Open curEmp
Fetch next from curEmp into @EmpId
While @@FETCH_STATUS = 0
Begin
Print @EmpId
Fetch next from curEmp into @EmpId
End
Close curEmp
Deallocate curEmp
Open the cursor,
Fetch record row by row,
Close cursor,
Deallocate cursor.
Example of a cursor
Declare @EmpId int
Declare curEmp CURSOR READ_ONLY FOR SELECT EmpId FROM Employee
Open curEmp
Fetch next from curEmp into @EmpId
While @@FETCH_STATUS = 0
Begin
Print @EmpId
Fetch next from curEmp into @EmpId
End
Close curEmp
Deallocate curEmp
Previous Question | Next Question |
Explain the ways to controlling Cursor Behavior? | What is cursor in MS SQL Server? |