The FORWARD_ONLY Cursor in SQL Server does not support scrolling. This SQL FORWARD_ONLY cursor can only move from the first row to last and does not support the other way (scrolling backward). It means the SQL FORWARD_ONLY Cursors support the FETCH_ONLY option, and it will return an error for all the remaining FETCH options.
SQL Server allows us to use the STATIC, DYNAMIC, or KEYSET keywords along with the FORWARD_ONLY Cursor. And if you omit any of these keywords, then SQL Server assumes it as DYNAMIC.
For this Create a forward_only Cursor in SQL Server demonstration, We use the below-shown table. As you can see, our Employee table holds 14 records
Forward_Only Cursor in SQL Server Example 1
In this example, we will show you how to declare and open a forward_only cursor in SQL Server. And here, we will use the different FETCH options to demonstrate the supporting fetch options.
DECLARE forward_employee_cursor CURSOR FORWARD_ONLY FOR SELECT * FROM [EmployeeTable] OPEN forward_employee_cursor FETCH NEXT FROM forward_employee_cursor;
Below statement will declare the forward_only cursor called forward_employee_cursor for all the records in Employee table
DECLARE forward_employee_cursor CURSOR FORWARD_ONLY FOR SELECT * FROM [EmployeeTable]
Below statement will open the declared cursor
The next statement will fetch, or return the next record from the forward_employee_cursor cursor.
FETCH NEXT FROM forward_employee_cursor;
Although our employee table has 14 records, the cursor is retrieving one record. It is because SQL Server FETCH NEXT will fetch only one row from the cursor, and if you want all, then use Loops.
Let me use the FETCH FIRST option. As you can see, this is throwing an error.
Let me use the FETCH LAST option.
Next, we used the FETCH PRIOR option.
Forward_Only Cursor in SQL Server example 2
Let us see how to create FORWARD_ONLY STATIC Cursors and FORWARD_ONLY DYNAMIC Cursors. For this, we are using the DECLARE CURSOR Statement, and within that, we use the WHILE LOOP to loop over the cursor elements and perform updates
SET NOCOUNT ON -- Declaring the Variables DECLARE @EmpID INT, @EmpName VARCHAR(50), @EmpEducation VARCHAR(50), @EmpOccupation VARCHAR(50), @EmpYearlyIncome DECIMAL (10, 2), @EmpSales DECIMAL (10, 2); DECLARE forward_employee_cursor CURSOR FORWARD_ONLY STATIC FOR SELECT [ID] ,[Name] ,[Education] ,[Occupation] ,[YearlyIncome] ,[Sales] FROM EmployeeTable ORDER BY Occupation OPEN forward_employee_cursor IF @@CURSOR_ROWS > 0 BEGIN FETCH NEXT FROM forward_employee_cursor INTO @EmpID, @EmpName, @EmpEducation, @EmpOccupation, @EmpYearlyIncome, @EmpSales WHILE @@FETCH_STATUS = 0 BEGIN IF @EmpOccupation = N'Management' UPDATE [EmployeeTable] SET [YearlyIncome] = 999999, [Sales] = 15000 WHERE CURRENT OF forward_employee_cursor FETCH NEXT FROM forward_employee_cursor INTO @EmpID, @EmpName, @EmpEducation, @EmpOccupation, @EmpYearlyIncome, @EmpSales END END CLOSE forward_employee_cursor DEALLOCATE forward_employee_cursor SET NOCOUNT OFF GO
If you observe the cursor declaration, we are using the SQL FORWARD_ONLY STATIC cursor, and performing the UPDATE option. We already explained the remaining steps in our previous article. I suggest you refer Dynamic Cursor in SQL Server, and Keyset Cursor examples.
You can see that the error is stating: The Cursor is READ ONLY. It is because we mentioned the FORWARD_ONLY STATIC in the cursor declaration. And, STATIC Cursors does not support INSERT, DELETE, or UPDATE operations.
Please use the following SQL Query to check whether the Cursor has updated records in the Employee table or not.
SELECT [ID] ,[Name] ,[Education] ,[Occupation] ,[YearlyIncome] ,[Sales] FROM [EmployeeTable]
Let me change the cursor declaration from static to dynamic by writing: FORWARD_ONLY DYNAMIC. Remember, it will work even if you forgot the DYNAMIC keyword because it is the default keyword for the forward_only trigger
Now you can see the updated records.