The SQL and access databases under ASP randomly retrieve the code newid () of 10 records ()

Source: Internet
Author: User

MSSQL: Select top 10 * from [Table] order by newid ()

Access:

CopyCode The Code is as follows: 'Use Rs. Move
'Random 10 entries are allowed.
N = 10
'First, You need to determine whether the total number of records is less than 10. If the number is less than 10, then how much if n> 10 rs. recordcount then n = Rs. recordcount
Dim rannum
For I = 1 to n
Randomize ()
Rannum = int (Rs. recordcount * RND) + 1' to generate a random number
Rs. Move rannum 'move the cursor to the random number position
Response. Write I & "-" & RS ("title") & "<br/>" 'output content
Rs. Move-rannum
Next
'......

This is the case. Of course, this may lead to repeated records. You can change the sentence that generates a random number and declare a variable to store the generated random number, when a new random is generated, first determine whether it has been generated. If it has been generated, generate it again. This is simple. You can use instr and so on ~~

The idea is like this. Try it on your own. Another way to avoid repeated reading is to generate only one random number and then read the N records next to the random number. Of course, you also need to make a good judgment. the random number must be smaller than the total number of records-the number of records to be read.

Copy code The Code is as follows :'......
Dim N, rannum
N = 10
Randomize ()
Rannum = Rs. recordcount-n
If rannum <1 then rannum = 1
Rannum = int (rannum * RND) + 1

Rs. Move rannum
For I = 1 to 10
Response. Write I & "-" & RS ("title") & "<br/>" 'output content
Rs. movenext
Next
'......

We recommend that you write a simple function to generate and Judge random numbers that are not repeated.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.