Use cursors!
Update:
BTW: These dynamic where clauses are very expensive because the optimizer cannot optimize them.
Better way: Generate SQL-Statements with a fixed filter, and execute them from within a procedure. This is much more performant. There was an article in SQL - mag in the black belt sql column recently.
Maybe I should meditate someday about "SQL-cursors for the unwary" or something like that.