Use SQL statement 8 in ASP: Random Number

Source: Internet
Author: User
Use SQL statement 8 in ASP: Random Number
Source:
Creating a randomly generated HTML code is a fairly easy-to-implement ASP feature. You may have created "One post per day", rolling advertisements, and so on. You only need to embellish your website with a little bit of content, which will make your website grow increasingly popular.

For the data stored in the database, random numbers provide the above results, but they may be too slow. You cannot ask ASP to "find a random number" and print it out. In fact, a common solution is to create a loop as follows:

Randomize

Rnumber = int (rndx 499) + 1

While not objrec. EOF

If objrec ("ID") = rnumber then

... The execution script...

End if

Objrec. movenext

Wend

This is easy to understand. First, you get a random number ranging from 1 to 500 (assuming 500 is the total number of records in the database ). Then, you traverse each record to test the id value and check whether it matches the rnumber. If conditions are met, the code starting with the then keyword is executed. If your rnumber is equal to 495, it takes a long time to repeat the database. Although the number 500 looks a little larger, it is still a small database than a more robust enterprise solution, which usually contains thousands of records in a database. At this time, it will not die?

With SQL, you can quickly find an accurate record and open a recordset that only contains the record, as shown below:

Randomize

Rnumber = int (rndx 499) + 1

SQL = "select * from MERs where id =" & rnumber

Set objrec = objconn. Execute (SQL)

Response. writernumber & "=" & objrec ("ID") & "& objrec (" c_email ")

You do not need to write rnumber and ID. You only need to check the matching conditions. As long as you are satisfied with the work of the above code, you can operate "random" records on demand. Recordset does not contain any other content, so you can quickly find the record you need, which greatly reduces the processing time.

Random Number again

Now you are determined to squeeze the last drop of the random function, you may retrieve multiple random records at a time or want to use records within a certain random range. By extending the standard random example above, you can use SQL to cope with the above two situations.

To retrieve several randomly selected records and place them in the same recordset, you can store three random numbers and query the database to obtain records matching these numbers:

SQL = "select * from MERs where id =" & rnumber & "or ID =" & rnumber2 & "or ID =" & rnumber3

If you want to select 10 records (maybe the list of 10 links at each page loading), you can use between or mathematical equations to select the first record and the appropriate number of incremental records. This operation can be completed in several ways, but the SELECT statement only shows one possibility (the ID here is the automatically generated number ):

SQL = "select * from MERs where ID between" & rnumber & "and" & rnumber & "+ 9"

Note: The above code is not executed to check whether there are 9 concurrent records in the database. If you need to ensure that 10 records are selected at a time, you must further design the query.

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.