|
|
Terms of Agreement:
By using this article, you agree to the following terms...
1) You may use
this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame.
4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description. | <%
'First we will create our recordset.
'
strSQLGetRs = "Select * from myTable"
Set rsGetRs = Server.CreateObject("ADODB.Recordset")
rsGetRs.Open strSQLGetRs,Conn,1,3
'Now lets get recordcount.
intRowCount = Cint(rsGetRs.Recordcount)
rsGetRs.MoveFirst
'And now we will choose one of them
Randomize Timer
intRandomRecord = Int(Rnd * intRowCount)
'Then lets move our cursor to that recor
' d...
rsGetRs.Move intRandomRecord
'That's it.
%>
http://www.clubasp.net
Serkan YOGURAN
serkan@clubasp.net | |
|
Report Bad Submission |
|
|
Your Vote! |
See Voting Log |
|
Other User Comments |
6/12/2002 9:50:17 AM:undercodex Simple method
|
6/14/2002 10:07:25 PM:Ray Powell Good Job! Now lets see if anyone can
tell me how to retrieve records from a
recordset in a random order.
|
10/6/2002 6:09:34 PM: Sure,
Use the intRowCount as the
number of times to pull
records.
IE.
For i = 1 To
intRowCount
rsGetRs("rsID")
Next
Of course you
may get duplicates, but results will be
retrieved in totally random order.
|
12/17/2002 1:35:16 AM: just great
|
4/10/2003 11:14:40 PM: Is there any way that we can get only a
single record randomly in a recordset
instead of getting the entire
table
strSQLGetRs = "Select * from
myTable"
rsGetRs.Open
strSQLGetRs,Conn,1,3
Because if our
table contains 1000's of records then
it will take long long time if we take
a random record time by time for
different users.
Reply soon
|
12/25/2003 12:59:39 AM: great but this
line
rsGetRs.MoveFirst
seems useless
|
1/5/2004 4:31:40 PM:G-Man Here's some SQL code that will return
one random record rather than filling
an entire recordset. Still, this
method behind the scenes is basically
building a sort of recordset and
assigning a new id to each record.
Nonetheless, someone asked for an SQL
method and here one is:
SELECT TOP
1 *, NEWID() AS new_id
FROM
myTable
ORDER BY new_id
|
|
Add Your Feedback! |
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.
NOTICE: The author of this article has been kind enough to share it with you. If you have a criticism, please state it politely or it will be deleted.
For feedback not related to this particular article, please click here. |
|