'Why I can't create a cursor in SQL?

Searching the entire internet for the corect syntax for creating a cursor in sql. Begin with this:

DECLARE cursorName CURSOR FOR
SELECT Name
FROM Student;

continue with this: BEGIN
DECLARE cursorNume CURSOR FOR
SELECT Nume
FROM Student;
END;

And still error. I am using Datagrip. Can anyone help me?



Solution 1:[1]

https://www.sqlservertutorial.net/sql-server-stored-procedures/sql-server-cursor/

This link offers the steps to use a cursor in the database explaining all the baby steps involved. Here's how I have followed it:

DECLARE
@current_transaction_id bigint;
    
DECLARE 
cursor_for_transactionIds CURSOR FOR SELECT ProfileId FROM EHR.Profiles;
    
OPEN cursor_for_transactionIds; 
FETCH NEXT FROM cursor_for_transactionIds INTO @current_transaction_id;

WHILE @@FETCH_STATUS = 0 
BEGIN 

PRINT @current_transaction_id 
    
FETCH NEXT FROM cursor_for_transactionIds into @current_transaction_id; 
END;

CLOSE cursor_for_transactionIds; 
DEALLOCATE cursor_for_transactionIds;

Although it has been quite a long when this question was asked. Sharing the solution to the question for those who'll encounter the same issue in the future. This is why you've landed on this question.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Saima