• + 0 comments
    with cte_r as(
        select 2 as n 
        union all 
        select n+1 from cte_r where n < 1000  
    ),
    
    check_prime as(
        select a.n 
        from cte_r a 
        left join cte_r b 
        on b.n >= 2 
            and b.n <= sqrt(a.n) 
            and a.n % b.n = 0
        where b.n is null
    )
    SELECT STRING_AGG(n, '&') 
           WITHIN GROUP (ORDER BY n)
    FROM check_prime
    OPTION (MAXRECURSION 1000);