这里是从 Access 中取随机记录,在 SQL Server 中,可以用另一种方法,请参阅:如何随机选取n条记录或者对记录作随机排序。
<%
'cn为connection对象
'rs为recordset对象
'recordCount为记录总数
'i代表记录数
'j代表重试次数,防止进入死循环
dim i,j
randomize()
do while i<8 and j<30
//8条记录
sql="select F1,F2 from T1 where id="&rnd()*recordCount\1
rs.open sql,cn,1,1
if not (rs.bof and rs.eof) then
i=i+1
response.write("....")
end if
rs.close
j=j+1
loop
%>
当记录数相对于所取记录数比较小时,易出现重复取某一记录,要克服这一点就得再加几句了...