Quick Search for:  in language:    
code,helps,random,record,from,your,table
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
ASP/ VbScript Stats

 Code: 196,174. lines
 Jobs: 109. postings

 How to support the site

 
Sponsored by:

 
You are in:
 
Login


 

 


Latest Code Ticker for ASP/ VbScript
Click here to see a screenshot of this code!Gallery Image with Zoom
By Marcelo Valle Franco on 1/12

(Screen Shot)

ASP Photo Competition
By Saul Bryan on 1/12


Execute DTS package
By Himadrish Laha on 1/12


Connecting to a MySQL with ADO, DAO and RDO
By Ahmed Magdy Ezzeldin on 1/11


Automatic Form Insertion To Database
By Yasar Bayar on 1/11


Click here to see a screenshot of this code!Find Closest Physical Location (based on zip code, etc)
By Mark Kahn on 1/11

(Screen Shot)

String --> SQL Search Query
By Mark Kahn on 1/11


Replace text/Numbers for Images...
By Kenneth Nilsson on 1/9


INSERT sentence generator
By Eitan Rousso on 1/8


Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!

Affiliate Sites



 
 
   

Getting a random record from a table

Print
Email
 

Submitted on: 6/11/2002 3:55:01 AM
By: Serkan YOGURAN  
Level: Beginner
User Rating: By 4 Users
Compatibility:ASP (Active Server Pages)

Users have accessed this article 2759 times.
 

 
     This code helps you to get a random record from your table.

 
 
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
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
Your Vote!

What do you think of this article(in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments
6/12/2002 9:50:17 AM:undercodex
Simple method
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
12/17/2002 1:35:16 AM:
just great
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
12/25/2003 12:59:39 AM:
great but this line rsGetRs.MoveFirst seems useless
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | ASP/ VbScript Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.