ASP Read Access database random Records method __ Database

Source: Internet
Author: User
Tags access database

Check some posts for the MSSQL database can generally use such statements:
<%
Select top * from SomeTable ORDER by NewID ()
%>


But if it's an Access database, there's no good workaround.

Method One: Someone uses the following code to first extract the specified number of random numbers as the maximum number of records, and then make the record IDs with these random numbers.
<%
Dim n,j
Dim su ()
Dim a,b,k
b=myrs. RecordCount
Randomize
ReDim SU (Index_n)
SU (1) =int ((b * Rnd) + 1)
For n=2 to Index_n
A=int ((b * Rnd) + 1)
For J=1 to N
Do While A=su (j)
A=int ((b* Rnd) + 1)
J=1
Loop
Next
SU (n) =a
Next
%>


There are some problems with this approach, that is, when the ID is not contiguous, it is possible that some random number does not exist in the ID sequence. The maximum value of the ID is not necessarily equal to the total record value, so some record IDs are always ignored.

Method Two: Someone uses an SQL statement to solve this problem:
<%
Select top * FROM table ORDER by int (rand () *50)
%>


If this statement works, it would be a good idea. I test this statement did not pass, if anyone knows where there is a problem please let me know, thank you very much.

Method Three: There is such a way, I feel good:
<%
Yourstr= "*1*3*4*6*12* ..."
Sql= "SELECT top * Form Yourdb where InStr (' * ' &id& ' * ', '" &yourstr& ') <>0 "
%>


YOURSTR can generate a random number more than the specified number of random numbers as well, which can eliminate the lack of records.

Method Four:
<%
N=10 ' Take any 10 records
Set rs = Server.CreateObject ("Adodb.recordset")
sql = "SELECT * FROM table"
Rs.Open sql,conn,1,1
Count=rs.recordcount ' Total records
If Count<>empty Then
Randomize
For I=1 to n ' Loops n times
Num=fix (rnd*count) ' num is the number of randomly generated rows of records, with fix () so that it is not greater than the count value
Rs.move num ' Move to change random row
Response.Write RS (0) ' out of this record
Rs.movefirst ' Don't forget to move the pointer to the first one.
Next
End If
Rs.close
Set rs=nothing
%>

Related Article

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.