DATETIME SELECT SELECT INTO DATE PAD STRING DYNAMIC SQL CURSOR MONEY FORMAT PERCENT STORED PROCEDURE SQL SERVER AGENT JOB OPTIMIZATION WHILE LOOP OVER PARTITION BY UPDATE
 
 
SITE SEARCH SQLUSA.com VIDEOS
 
 
SQL E/BOOKS   WORLD, USA & SQL NEWS   FORMAT
SCRIPTS SQL 2005 SQL 2008 ARTICLES
How to generate Prime Numbers?

Execute the following Microsoft SQL Server T-SQL scripts in SSMS Query Editor for demonstrating a way for Prime Number list generation.

 

-- Prime number generator UDF - user-defined function for primes

CREATE FUNCTION fnPrimeNumbers ( @Limit INT)

RETURNS @Result TABLE  (Prime INT)

BEGIN

WITH cteSequence ( IntNo)

     AS (SELECT 1

         UNION ALL

         SELECT IntNo + 1

         FROM   cteSequence

         WHERE  IntNo < @Limit)

INSERT @Result

SELECT PrimeNo = N2.IntNo

FROM   cteSequence N1

       INNER JOIN cteSequence N2

    ON  N2.IntNo % N1.IntNo > 0

       AND N2.IntNo % 2 > 0

       AND N1.IntNo < N2.IntNo

       AND N2.IntNo > 1

       AND N1.IntNo >= 1

GROUP  BY N2.IntNo

HAVING ( N2.IntNo - COUNT(*) ) = 2

OPTION ( MAXRECURSION 0);

RETURN ;

END

GO

SELECT * FROM dbo.fnPrimeNumbers (5000)

/* PrimeNo

3

5

7

11

13

17

19

23  ....*/

------------ 

Exam Prep 70-461
SQL 2016 DESIGN & PROGRAMMING
 
 
 
DATETIME SELECT SELECT INTO DATE PAD STRING DYNAMIC SQL CURSOR MONEY FORMAT PERCENT STORED PROCEDURE SQL SERVER AGENT JOB OPTIMIZATION WHILE LOOP OVER PARTITION BY UPDATE