SQL CURSOR and LOOPS

Learn how to write a SQL CURSOR or loop to process records one row at a time. Although this is not a best practice for most applications, cursors and loops provide value during specific use cases.

Title Author Date
SQL Server Cursor Example Jeremy Kadlec2026-05-04
sp_msforeachdb to Execute the Same Command on all SQL Server Databases Tim Ford2025-12-30
SQL WHILE LOOP Examples and Alternatives Daniel Calbimonte2025-05-28
SQL Loop through Table Rows without Cursor Daniel Farina2025-05-27
Replace SQL Cursors with Set Based Operations – OUTPUT and MERGE Jared Westover2024-05-30
Replace a SQL WHILE Loop with Set Based Logic in T-SQL Jared Westover2024-04-09
Scroll Cursor in SQL Server Examples Anirudh Kumar Bhudhiraj2022-10-14
SQL FOR Loop Alternatives Koen Verbeeck2021-12-13
SQL WHILE Loop Avoid WHILE 1 = 1 Aaron Bertrand2021-09-08
Different Ways to Write a Cursor in SQL Server Simon Liew2020-02-05
Execute Command in Each SQL Server Database with sp_ineachdb Aaron Bertrand2018-10-04
Making a more reliable and flexible sp_MSforeachdb Aaron Bertrand2018-07-24
Iterate through SQL Server database objects without cursors Arshad Ali2009-12-21