-- Declare the variables to store the values returned by FETCH.
DECLARE @var INT
DECLARE _Cursor CURSOR FOR
SELECT NameOfField FROM [NameOfDatabase].[dbo].[NameOfTable]
OPEN _Cursor
-- Perform the first fetch and store the values in variables.
-- Note: The variables are in the same order as the columns
-- in the SELECT statement.
FETCH NEXT FROM _Cursor
INTO @var
-- Check @@FETCH_STATUS to see if there are any more rows to fetch.
WHILE @@FETCH_STATUS = 0
BEGIN
-- Concatenate and display the current values in the variables.
PRINT 'ID: ' + @var
-- This is executed as long as the previous fetch succeeds.
FETCH NEXT FROM _Cursor
INTO @var
END
CLOSE _Cursor
DEALLOCATE _Cursor
GO