There is some controversy about using cursors in TSQL. Overall, I believe they tend to tie up resources and are seen as bad. So how do you get around using them? Here’s how.
loop
(If you want to AVOID cursors, see: TSQL – Loop Through Result Set WITHOUT Cursors) This is the basic query text to use whenever you need to loop through a result set in SQL Server using TSQL. In this example we are getting every database name and printing it out.