Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Infinite loop in SQL?

There is some way to implement a infinite loop in SQL?? I was thinking on some like a select inside another one, recursively... (Maybe im talking foolishness)

like image 385
Diogo Avatar asked May 10 '11 18:05

Diogo


2 Answers

You can do a recursive infinite loop with a CTE:

;with rec as
        (
        select  1 as n
        union all
        select  n + 1
        from    rec
        )
select  n
from    rec

By default, SQL Server would stop at 100; you can make it loop forever with:

option  (maxrecursion 0)
like image 110
Andomar Avatar answered Sep 27 '22 21:09

Andomar


WHILE 1=1
BEGIN
SELECT 'This will go forever'
END
like image 28
JNK Avatar answered Sep 27 '22 22:09

JNK