In Oracle, we can use
FOR employee_rec in (select id from mytbl)
LOOP
DBMS_OUTPUT.PUT_LINE(employee_rec.id);
END LOOP;
In SQL Server, do we have something similar?, if not, how to do this loop?
In Oracle, we can use
FOR employee_rec in (select id from mytbl)
LOOP
DBMS_OUTPUT.PUT_LINE(employee_rec.id);
END LOOP;
In SQL Server, do we have something similar?, if not, how to do this loop?
It's a little more verbose in SQL Server, if you want to do it right:
DECLARE @id INT;
DECLARE c CURSOR LOCAL STATIC READ_ONLY FORWARD_ONLY
FOR SELECT id FROM mytbl;
OPEN c;
FETCH NEXT FROM c INTO @id;
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT @id;
FETCH NEXT FROM c INTO @id;
END
CLOSE c;
DEALLOCATE c;
However, usually we tend to avoid loops and cursors, since a relational database excels at set-based operations (in almost cases). There are exceptions of course, like any rule, but if I had to guess I would suspect that you don't need to loop to do what you are trying to do. What are you really trying to do? Why do you need to process each row individually?