The FORWARD_ONLY Cursor in SQL Server does not support scrolling. This cursor can only move from the first row to last and does not support the other way (scrolling backwards). It means, SQL FORWARD_ONLY Cursors support FETCH_ONLY option, and it will an error for all the remaining FETCH options.
In this article, we will show you, How to Create a forward_only Cursor in SQL Server with an example. For this demonstration, We are going to use the below-shown table.
As you can see from the below screenshot that 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 option to demonstrate the supporting fetch options.
USE [SQL Tutorial] GO 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
Next statement will fetch, or return the next record from the forward_employee_cursor cursor.
FETCH NEXT FROM forward_employee_cursor;
Though our employee table has 14 records, the cursor is retrieving 1 record. This is because, FETCH NEXT will fetch only one record 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
In this example, we will show you, 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 will 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 carefully, we are using 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 example.
You can see that the error is stating: The Cursor is READ ONLY. This 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 any of the records in the Employee table or not.
USE [SQL Tutorial] GO 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.
Thank you for Visiting Our Blog