This example uses MS SQL Server. As a regular maintenance routine, you may want to run DBCC CheckTable on all the tables in your database (or some other type of operation). Instead of hand-coding all the tables into a procedure, here's a way to loop through all the tables, perform a DBCC CheckTable (or any other operation) without using a cursor. -- Declare counter variables -- @min will be the "current" record being examined -- @max is the highest record. declare @min int, @max int -- Declare a variable to hold the name of the table: declare @tablename sysname -- Set the initial values for the counter variables -- @min is the lowest record meeting our condition, -- @max is the highest: select @min = min(id), @max = max(id) from sysobjects where type = 'u' -- Establish loop conditions: -- Continue running until @min is equal to @max while @min <= @max begin -- Populate the tablename variable based on -- the current value of @min select @tablename = name from sysobjects where id = @min -- Execute the DBCC (or other operation) dbcc checktable (@tablename) -- Advance the counter. -- Note the where condition is identical to the -- initial loop condition with one additional -- bit: "and id > @min" select @min = min(id) from sysobjects where type = 'u' and id > @min -- Exit the loop end While this example uses MS SQL and the sysobjects table, it is easy enough to adjust the basic structure to MySQL or any other table that has an auto-incrementing, unique integer. If you don't have one of those, or you want to sort the records some other way, create a temp table with it's own auto-incrementing integer and the Primary Key of the table you want to adjust (make sure you drop the temp table when you're finished). I am a firm believer that a cursor is never the right answer. This is a much simpler and cleaner way to achieve the same basic result without all the extra over-head a cursor places on the database engine.