Author |
Topic |
mahdi87_gh
Yak Posting Veteran
72 Posts |
Posted - 2009-07-17 : 06:04:12
|
HiThere is a table Composed of an identity field(ID) and others.I wanna select 3 random records from this table and show them in a c# application. to do so, firstly I have to figure out how many records exist in table. I can do this with select count(*) statement. For example, the result should be something like 11,23,6. Now I should select the 11th,23rd,6th records from the table. I can do these operations separately in two commands in the c# application. I mean to find out the numbers through a command and then choose 3 random numbers in the range in the C# application and then select those records from the table. But I want do all these done with one command and in the sql server itself. I mean the random numbers be selected in the sqlserver itself.Thanks to every body. ****<< I Love MTN.SH >>**** |
|
senthil_nagore
Master Smack Fu Yak Hacker
1007 Posts |
Posted - 2009-07-17 : 06:32:00
|
Try thisDECLARE @maxRandomValue TINYINT = 100 , @minRandomValue TINYINT = 0 SELECT CAST(((@maxRandomValue + 1) - @minRandomValue) * RAND() + @minRandomValue AS TINYINT) AS 'randomNumber'Senthil.C------------------------------------------------------[Microsoft][ODBC SQL Server Driver]Operation canceledhttp://senthilnagore.blogspot.com/ |
 |
|
rajdaksha
Aged Yak Warrior
595 Posts |
Posted - 2009-07-17 : 06:35:47
|
quote: Originally posted by senthil_nagoreDECLARE @maxRandomValue TINYINT = 100 , @minRandomValue TINYINT = 0
we cannot assign a default value to a local variable..right-------------------------R.. |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-07-17 : 06:53:43
|
[code]select top 3 *from yourtableorder by newid()[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-07-17 : 07:17:10
|
quote: Originally posted by rajdaksha
quote: Originally posted by senthil_nagoreDECLARE @maxRandomValue TINYINT = 100 , @minRandomValue TINYINT = 0
we cannot assign a default value to a local variable..right-------------------------R..
Except you are on SQL Server 2008MadhivananFailing to plan is Planning to fail |
 |
|
rajdaksha
Aged Yak Warrior
595 Posts |
Posted - 2009-07-17 : 07:25:52
|
Hiyes i have 2005 box...-------------------------R.. |
 |
|
mahdi87_gh
Yak Posting Veteran
72 Posts |
Posted - 2009-07-17 : 08:30:11
|
i have 2005 box too.what i have to do?****<< I Love MTN.SH >>**** |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-07-17 : 08:43:33
|
quote: Originally posted by mahdi87_gh i have 2005 box too.what i have to do?****<< I Love MTN.SH >>****
Did you try khtan's query?MadhivananFailing to plan is Planning to fail |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-07-17 : 08:44:43
|
quote: Originally posted by rajdaksha Hiyes i have 2005 box...-------------------------R..
Default declaration works only from 2008 versionSee my previous replyMadhivananFailing to plan is Planning to fail |
 |
|
rajdaksha
Aged Yak Warrior
595 Posts |
Posted - 2009-07-17 : 08:47:49
|
HiDECLARE @maxRandomValue TINYINT SET @maxRandomValue =100-------------------------R.. |
 |
|
mahdi87_gh
Yak Posting Veteran
72 Posts |
Posted - 2009-07-17 : 11:26:23
|
thanks to all friendsthe newid() works****<< I Love MTN.SH >>**** |
 |
|
|