| Author | Topic | 
                            
                                    | mahdi87_ghYak 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_nagoreMaster 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/ |  
                                          |  |  | 
                            
                       
                          
                            
                                    | rajdakshaAged Yak Warrior
 
 
                                    595 Posts | 
                                        
                                          |  Posted - 2009-07-17 : 06:35:47 
 |  
                                          | quote:we cannot assign a default value to a local variable..right-------------------------R..Originally posted by senthil_nagoreDECLARE @maxRandomValue TINYINT = 100	, @minRandomValue TINYINT = 0
 
 |  
                                          |  |  | 
                            
                       
                          
                            
                                    | khtanIn (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]
 |  
                                          |  |  | 
                            
                       
                          
                            
                                    | madhivananPremature Yak Congratulator
 
 
                                    22864 Posts | 
                                        
                                          |  Posted - 2009-07-17 : 07:17:10 
 |  
                                          | quote:Except you are on SQL Server 2008MadhivananFailing to plan is Planning to failOriginally posted by rajdaksha
 
 quote:we cannot assign a default value to a local variable..right-------------------------R..Originally posted by senthil_nagoreDECLARE @maxRandomValue TINYINT = 100	, @minRandomValue TINYINT = 0
 
 
 |  
                                          |  |  | 
                            
                       
                          
                            
                                    | rajdakshaAged Yak Warrior
 
 
                                    595 Posts | 
                                        
                                          |  Posted - 2009-07-17 : 07:25:52 
 |  
                                          | Hiyes i have 2005 box...-------------------------R.. |  
                                          |  |  | 
                            
                       
                          
                            
                                    | mahdi87_ghYak 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 >>**** |  
                                          |  |  | 
                            
                       
                          
                            
                                    | madhivananPremature Yak Congratulator
 
 
                                    22864 Posts | 
                                        
                                          |  Posted - 2009-07-17 : 08:43:33 
 |  
                                          | quote:Did you try khtan's query?MadhivananFailing to plan is Planning to failOriginally posted by mahdi87_gh
 i have 2005 box too.what i have to do?****<< I Love MTN.SH >>****
 
 |  
                                          |  |  | 
                            
                       
                          
                            
                                    | madhivananPremature Yak Congratulator
 
 
                                    22864 Posts | 
                                        
                                          |  Posted - 2009-07-17 : 08:44:43 
 |  
                                          | quote:Default declaration works only from 2008 versionSee my previous replyMadhivananFailing to plan is Planning to failOriginally posted by rajdaksha
 Hiyes i have 2005 box...-------------------------R..
 
 |  
                                          |  |  | 
                            
                       
                          
                            
                                    | rajdakshaAged Yak Warrior
 
 
                                    595 Posts | 
                                        
                                          |  Posted - 2009-07-17 : 08:47:49 
 |  
                                          | HiDECLARE @maxRandomValue TINYINT   SET @maxRandomValue =100-------------------------R.. |  
                                          |  |  | 
                            
                       
                          
                            
                                    | mahdi87_ghYak Posting Veteran
 
 
                                    72 Posts | 
                                        
                                          |  Posted - 2009-07-17 : 11:26:23 
 |  
                                          | thanks to all friendsthe newid() works****<< I Love MTN.SH >>**** |  
                                          |  |  | 
                            
                            
                                |  |