Calculating Prime Numbers With One Query


Introduction

I want to share the solution to the problem of searching for prime numbers using T-SQL. The problem was proposed on a reputable educational web resource. The problem is defined as follows: write all prime numbers less than or equal to 1000.

Of course, you can create a loop where numbers are sequentially checked against the definition of prime numbers and then displayed. But this is ugly and not interesting. It would be nice to formulate a single query that will solve the problem. Let’s try to do this.

Getting a set of numbers

How do I get a