Click here to Skip to main content
15,904,822 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

Suppose we have a set of data with 5000 records and we need to write a query that helps us every time we are required to fetch 50 Random rows from that table and tat only one time get question.



Thanks & Regards,
Posted
Comments
Praveen Kumar Upadhyay 3-Dec-14 3:48am    
Means the records which are picked should not get picked next time? If yes then how you are gonna remember picked records.
Pratham4950 3-Dec-14 4:48am    
i got the ides, by update that picked record on runtime. and the code is

con.Open();
string strCmd = "SELECT top 5 * FROM quesheet where status = 0 ORDER BY NEWID() ";
SqlCommand cmd = new SqlCommand(strCmd, con);
SqlDataAdapter da = new SqlDataAdapter(strCmd, con);
DataTable dt = new DataTable();
da.Fill(dt);
foreach (DataRow row in dt.Rows)
{
conobj.con_open();
conobj.update_que("update quesheet set status ='1' where id = '"+ row.Field<int>(0) +"' ");
}
cmd.ExecuteReader();
dataGridView1.DataSource = dt;
con.Close();
BillWoodruff 3-Dec-14 5:36am    
Is it correct that you wish to get a certain number of Rows each time, and each time have the set of Rows be Rows that have not been accessed before ?

If that's the case, let me know, I have some ideas and code for that case.

Something like this:

SQL
select top 5000 * from TABLENAME order by newid()
 
Share this answer
 
Hi
Try this
SQL
select top 50 * from  tbl order by NEWID()
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900