A static cursor populates the result set at the time of cursor creation and query result is cached for the lifetime of the cursor. A static cursor can move forward and backward direction. A static cursor is slower and use more memory in comparison to other cursor. Hence you should use it only if scrolling is required and other types of cursors are not suitable.
No UPDATE, INSERT, or DELETE operations are reflected in a static cursor (unless the cursor is closed and reopened). By default static cursors are scrollable. SQL Server static cursors are always read-only.
- SET NOCOUNT ON
- DECLARE @Id int
- DECLARE @name varchar(50)
- DECLARE @salary int
- DECLARE cur_emp CURSOR
- STATIC FOR
- SELECT EmpID,EmpName,Salary from Employee
- OPEN cur_emp
- IF @@CURSOR_ROWS > 0
- BEGIN
- FETCH NEXT FROM cur_emp INTO @Id,@name,@salary
- WHILE @@Fetch_status = 0
- BEGIN
- PRINT 'ID : '+ convert(varchar(20),@Id)+', Name : '+@name+ ', Salary : '+convert(varchar(20),@salary)
- FETCH NEXT FROM cur_emp INTO @Id,@name,@salary
- END
- END
- CLOSE cur_emp
- DEALLOCATE cur_emp
- SET NOCOUNT OFF
No comments:
Post a Comment