Hello colleagues and MS SQL experts,
I would like to ask if it is possible to create such cursor in MS SQL so that it would be possible to get the selected cursors’ columns in a loop directly from cursor variable without defining variable for each selected column?
For example in Oracle I get data directly from cursor's variable.
FOR cur1 IN (SELECT emp_name, emp_phone FROM employees) loop
dbms_output.put_line('Employee ' cur1.emp_name || ' Phone ' || cur1.emp_phone);
end loop;
In MS SQL example I have declare variables, i.e. @name, @phone for each column.
DECLARE cur1 CURSOR FOR SELECT emp_name, emp_phone FROM employees;
DECLARE @name nvarchar(50);
DECLARE @phone nvarchar(50);
OPEN cur1;
FETCH NEXT FROM cur1 INTO @name, @phone;
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT 'Employee ' + @name + ' Phone ' + @phone;
FETCH NEXT FROM cur1 INTO @name, @phone;
END
CLOSE cur1;
DEALLOCATE cur1;
Maybe there is some other, more simple way, in MS SQL, similar to Oracle's?
Thank You.