Skip to content

CURSOR

DECLARE db_cursor CURSOR FOR
SELECT name
FROM requirements
OPEN db_cursor
FETCH NEXT FROM db_cursor
WHILE @@FETCH_STATUS = 0
BEGIN
FETCH NEXT FROM db_cursor
END;
CLOSE db_cursor;
DEALLOCATE db_cursor;
requirement1
requirement2
requirement3

In the above example, a cursor named db_cursor is declared and associated with a SELECT statement that fetches the names from the requirements table. The cursor is then opened, and a WHILE loop is used to iterate through the rows returned by the query. For each iteration, FETCH NEXT is called to move the cursor to the next row. When all rows have been processed, the cursor is closed and deallocated.