Syntax:
DECLARE <cursor_name> OPEN <cursor_name> FETCH NEXT FROM <cursor_name> WHILE @@FETCH_STATUS = 0 BEGIN; END; CLOSE <cursor_name> DEALLOCATE <cursor_name>
Example:
DECLARE @123 TABLE(Number INT NOT NULL);
INSERT INTO @123(Number)
VALUES(1), (2), (3);
DECLARE @XNumber INT;
DECLARE @XCursor AS CURSOR;
SET @XCursor = CURSOR
FOR SELECT Number
FROM @123;
OPEN @XCursor;
FETCH NEXT FROM @XCursor INTO @XNumber;
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT 'The number is ' + CAST(@XNumber AS VARCHAR(1));
FETCH NEXT FROM @XCursor INTO @XNumber;
END;
CLOSE @XCursor;
DEALLOCATE @XCursor;
