Define the steps to use Transact-SQL Cursor

Define the steps to use Transact-SQL Cursor.

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
Explain the cursor types
DYNAMIC: It reflects changes happened on the table while scrolling through the row........
Define the cursor lock types
Three types of locks READ ONLY: This prevents any updates on the table........
Cursor optimization tips
Close cursor when it is not required. You shouldn’t forget to deallocate cursor after closing it........
Post your comment