• Jason A. Long - Wednesday, December 13, 2017 2:18 PM

    Jeff Moden - Wednesday, December 13, 2017 5:27 AM

    unas_sasing - Tuesday, December 12, 2017 7:54 PM

    Hai all,

    thank you for helping me,

    Yes I would like to reverse from a result set 2,3,5,7,11,13,17,19,23,29, would like to be returned 29,23,19,17,13,11,7,5,3,2 instead. 

    and perhaps I've got the answer from @anthony.green and @tbailey 19088.

    Thank you very much.

    I will try the Syntax..

    I've not executed it but the original code will do it.  Instead of calling it with a 1, just call it with a 2.  And, seriously, instead of calling a proc every time you need Prime Numbers, which don't change, use the proc just once to build a table of Prime Numbers.  Your code that needs the prime numbers will be faster and easier to write.

    If you do insist on using a proc for this instead of such a "Helper Table", at least find a Prime Number generator that will be fast.  Use Peter "Peso" Larsson's code from the following link.  It will generate prime numbers up to 1,000,000 in only 2 to 4 seconds depending on your machine and cache.  It's the one that "Peso" posted with "This is about as easy and fast as possible, set-based" in it.

    Unless you're in the business of discovering new primes, don't bother doing any prime calculations at all... The interwebs has no shortage of list of pre-calculated primes.
    This site alone covers you from 0 to 1,000,000,000,000... http://www.primos.mat.br/indexen.html

    Amen to this.

    That said, there is a good Ben-Gan article about using a tally table function to calculate primes. I mention it because he talks about why he changed his tally table function to use a TOP clause instead of WHERE N <= @whatever. Its an interesting read: for those who use tally tables.

    http://www.itprotoday.com/microsoft-sql-server/virtual-auxiliary-table-numbers

    P.S. sorry for the plain text link. Im using my phone.

    "I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code."

    -- Itzik Ben-Gan 2001