PC Review


Reply
Thread Tools Rate Thread

Select a number of rows randomly in a table ?

 
 
Phan Tien
Guest
Posts: n/a
 
      24th Mar 2004
How can I select a number of rows randomly in a table in Microsoft Access or
SQL Server? I tried with many ways but I can't solve this problem Please
help me.
Thank you very much



 
Reply With Quote
 
 
 
 
Robin Tucker
Guest
Posts: n/a
 
      24th Mar 2004
In SQL server (this is not really VB related), I use this stored procedure
to return a random record:

ALTER PROCEDURE dbo.proc_TEST_Get_Random_Record
AS
BEGIN

DECLARE @ID INTEGER
DECLARE @rand_num INTEGER
DECLARE @num_recs INTEGER

SET NOCOUNT ON

/*
Work out size of table
*/

SELECT @num_recs = COUNT(*) FROM theTable

/*
Choose a random number to the extents of the table
*/

SET @rand_num = ROUND ( @num_recs * RAND () + 1, 0, 1 )

/*
Use a cursor to access the random record
*/

DECLARE Records CURSOR SCROLL static read_only FOR
SELECT ID FROM theTable
OPEN Records
FETCH ABSOLUTE @rand_num FROM Records INTO @ID
CLOSE Records
DEALLOCATE Records

/*
Select the record data.
*/

RETURN @ID
END


"Phan Tien" <(E-Mail Removed)> wrote in message
news:uU2$(E-Mail Removed)...
> How can I select a number of rows randomly in a table in Microsoft Access

or
> SQL Server? I tried with many ways but I can't solve this problem Please
> help me.
> Thank you very much
>
>
>



 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How can i randomly select 780 rows from 4000 rows of data =?Utf-8?B?YmJi?= Microsoft Excel Worksheet Functions 2 6th Jul 2007 09:21 PM
Re: How do I select a certain number of records randomly from a ta =?Utf-8?B?QWRhbVczOTk1?= Microsoft Access Queries 0 24th Aug 2005 02:56 PM
Is there a way to randomly select rows in Excel? =?Utf-8?B?U2FtYW50aGE=?= Microsoft Excel Misc 4 12th May 2005 03:59 PM
How do I select multiple rows randomly in MS Excel? =?Utf-8?B?VmFydW4=?= Microsoft Excel Misc 2 18th Apr 2005 09:10 AM
Select a number of rows randomly in a table ? Phan Tien Microsoft Access ADP SQL Server 0 24th Mar 2004 01:17 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:10 AM.