The best approach is to take advantage of the set-based nature of SQL and avoid cursors whenever you can. If you can rewrite a query using a cursor to one that uses JOIN
, you should always do so.
If you find a situation where you must iterate, it is usually better to do it on the database server. This is to avoid the overhead of repeated calls to the database that will occur if you are doing it in the application layer.
Update: Regarding whether you should iterate in the front end or not, it all depends on what you want to do within the loop. Display the data? Then sure, you probably want to itereate on the front end. If, however, you are iterating through the data in order to look something else up in the database, do a calculation, etc., you may be better off doing this on the db server. We'll need more specifics to make detailed recommendations, right now we can only speak in generalities with the info you have provided.