The SQL FAST_FORWARD Cursor is one of the fastest cursors we have. This cursor is a combination of FORWARD_ONLY, and READ_ONLY. It means, the cursor will only move from the first row to last and does not support the scrolling backwards.
In this article, we will show you, How to Create a FAST_FORWARD 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
Fast_Forward Cursor in SQL Server Example
In this example, we will show you, How to declare, and open a fast_forward cursor in SQL Server. And, we will use the different FETCH option to demonstrate the supporting fetch options.
-- SQL FAST_FORWARD Cursor Example USE [SQL Tutorial] GO DECLARE fast_forward_employee_cursor CURSOR FAST_FORWARD FOR SELECT * FROM [EmployeeTable] OPEN fast_forward_employee_cursor FETCH NEXT FROM fast_forward_employee_cursor;
Below statement will declare the fast_forward cursor called fast_forward_employee_cursor for all the records in Employee table
DECLARE fast_forward_employee_cursor CURSOR FAST_FORWARD FOR SELECT * FROM [EmployeeTable]
Next, we use the OPEN Cursor statement to open the declared cursor
The last statement will fetch, or return the next record from the fast_forward_employee_cursor cursor.
FETCH NEXT FROM fast_forward_employee_cursor;
Although the employee table has 14 rows, this cursor is retrieving 1 record. This is because, FETCH NEXT option will fetch only one record from the cursor, and if you want all then use Loops.
Next, we used the FETCH PRIOR option. As you can see, this is throwing an error.
Let me use the FETCH LAST option.
Let me use the FETCH FIRST option.
Thank you for Visiting Our Blog