CURSORS:

  • A cursor is a database object which is used to retrieve data from result set one row at a time.The cursor can be used when the data needs to be updated row by row.
  • @@fetch_status is the system variable which returns the status of the fetch statement.
  • If fetch is successful it return 0, otherwise it returns non-zero value.


Ex:- Write a program to display employee names and salaries using a cursor.

declare c1 cursor for
select ename, esal from employ
declare @ename varchar(20),@esal int
open c1
fetch next from c1 into @ename, @esal
while(@@fetch_status=0)
begin
print @ename+'earns'+ cast(@esal as varchar)
fetch next from c1 into @ename,@esal
end
close c1
deallocate c1

Comments

Popular posts from this blog