"displaying recs randomly in a table"
-
iam having an emp table and i want to display recs randomly each time i retrieve with regards ravi kiran
-
iam having an emp table and i want to display recs randomly each time i retrieve with regards ravi kiran
Retrieve what?
-
iam having an emp table and i want to display recs randomly each time i retrieve with regards ravi kiran
Are you talking about a System.Data.DataTable that's already populated and you want to pull a random record from it? Or are you talking about writing a select statement that randomly selects a row? If it's a datatable you could use Random.Next(DataTable.Rows.Count) to get a random integer with a maximum value set to the row count. If it's a sql query you want to write you could get a random number based on the RAND() function and the COUNT() function then write something like: DECLARE @Id INT, @Count INT, @Rnd INT SELECT @Count = COUNT(*) FROM table SET @Rnd = CAST((@Count*rand())+1 AS INT) SELECT TOP (@Count) @Id = Id FROM table ORDER BY CartNumber SELECT * FROM table WHERE Id = @Id If you're not using SQL 2005 you'll have to build the SELECT TOP query as a string and call EXECUTE instead.
-
iam having an emp table and i want to display recs randomly each time i retrieve with regards ravi kiran
on mssql: select top 100 * from yourtable order by newid()
-
Are you talking about a System.Data.DataTable that's already populated and you want to pull a random record from it? Or are you talking about writing a select statement that randomly selects a row? If it's a datatable you could use Random.Next(DataTable.Rows.Count) to get a random integer with a maximum value set to the row count. If it's a sql query you want to write you could get a random number based on the RAND() function and the COUNT() function then write something like: DECLARE @Id INT, @Count INT, @Rnd INT SELECT @Count = COUNT(*) FROM table SET @Rnd = CAST((@Count*rand())+1 AS INT) SELECT TOP (@Count) @Id = Id FROM table ORDER BY CartNumber SELECT * FROM table WHERE Id = @Id If you're not using SQL 2005 you'll have to build the SELECT TOP query as a string and call EXECUTE instead.
Oops, a small mistake. Change this: SELECT TOP (@Count) @Id = Id FROM table ORDER BY CartNumber to this: SELECT TOP (@Rnd) @Id = Id FROM table