Ms sql 2008 - MAXRECURSION
To stop a statement after a defined number of loops is called maxrecursion.
Below example is showing to stop infinite loop.when you CTE is going to 4th recursion it will throw an error and stop executing.If you put MAXRECURSION value too low it may be possible before your desire result and will throw an error.
WITH AamirHasan_CTE(id) AS
OPTION (MAXRECURSION 4);