[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 220
  • Last Modified:

Best Practice in Looping through the rows in a table

Hi there,

What is the best practice when it comes to iterating thru each row in a table?  Do I have to use CURSORS to achieve this or is there a better/more efficient way to do this?

I understand that using CURSORS is not a good programming practice.

For example, if I have a table that contains 500 rows or even maybe 5 million rows and i want to loop thru each row and then based on some logic as to perform some task.

1 Solution
SQL is a set-based language. Efficient SQL works with sets. Loops are a procedural construct, not a set-based construct. That's why loops are an inefficient way to work in a database. A cursor will allow you to loop over all rows. However, I think the key is in this part of your statement:
"based on some logic as to perform some task".

Chances are pretty good that "some logic" can be used to define subsets of the rows in the table. Then if you implement a procedure or run a series of statements that define the logic to select the subset on which you want to perform each different member of "some task". It will run more efficiently.

Does that help?

singhchAuthor Commented:
Well not really.  anyway your explanation is fine.

maybe i will post another question on a specific task that i want to achieve. And maybe you can advise how you would go about implementing the solution.


Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now