Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. "displaying recs randomly in a table"

"displaying recs randomly in a table"

Scheduled Pinned Locked Moved Database
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    ravikiranreddydharmannagari
    wrote on last edited by
    #1

    iam having an emp table and i want to display recs randomly each time i retrieve with regards ravi kiran

    P M J 3 Replies Last reply
    0
    • R ravikiranreddydharmannagari

      iam having an emp table and i want to display recs randomly each time i retrieve with regards ravi kiran

      P Offline
      P Offline
      Paddy Boyd
      wrote on last edited by
      #2

      Retrieve what?

      1 Reply Last reply
      0
      • R ravikiranreddydharmannagari

        iam having an emp table and i want to display recs randomly each time i retrieve with regards ravi kiran

        M Offline
        M Offline
        Mark J Miller
        wrote on last edited by
        #3

        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.

        M 1 Reply Last reply
        0
        • R ravikiranreddydharmannagari

          iam having an emp table and i want to display recs randomly each time i retrieve with regards ravi kiran

          J Offline
          J Offline
          jezemine
          wrote on last edited by
          #4

          on mssql: select top 100 * from yourtable order by newid()

          www.elsasoft.org

          1 Reply Last reply
          0
          • M Mark J Miller

            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.

            M Offline
            M Offline
            Mark J Miller
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups