SQL statement tip to randomize a Result Set (Order by NewId())

Contributed by
Rating: 5 stars5 stars5 stars5 stars5 stars / 7
March 26, 2002
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

Randomize SQL Result set


I was recently looking for a method that would randomize a list of links displayed on ASPFree.com.   Thanks to Scott Watermasysk (TripleASP.NET founder) passed along a tip that would randomize data coming from MS SQL Server.  If the SQL statement that is SELECT * FROM TABLE ORDER BY NewId() , the returning results are scrambled.  This was a nice way to re-arrange a result set.   This is example can be used either in ASP.NET or classic ASP.

WebPage code

RandomSQL.aspx
<%@ Page Language="VB" Debug="true" %>
<%@ Import Namespace="System.data" %>
<%@ Import Namespace="System.data.sqlclient" %>

<script language=vb runat=server>
Sub Page_Load(Sender as Object, E as EventArgs)
         Dim conn As SqlConnection = New SqlConnection("server=localhost;uid=sa;pwd=;database=databaseName")
         Dim cmd As SqlCommand = New SqlCommand("Select * From NewId Order by NewID()", conn)
         conn.Open()
         DataGrid1.DataSource = cmd.ExecuteReader
         DataGrid1.DataBind()
         conn.Close()
End Sub
</script>

<HTML>
         <HEAD><title>WebForm1</title></HEAD>
          <body>
              <form id="Form1" method="post" runat="server">
                   <asp:DataGrid id="DataGrid1" runat="server"></asp:DataGrid>
              </form>
          </body>
</HTML>

RandomSQL.asp

<%
         
Dim conn, rs
          strconn = "Driver={SQL Server};Description=sqldemo;SERVER=127.0.0.1;UID=sa;PWD=;DATABASE=databaseName"

          set conn = server.createobject("adodb.connection")
          conn.open strconn
          set rs = conn.execute("Select * from NewId Order by NewId()")
%>
<html>
           <body>
                       <%
                            Do While Not rs.EOF
                                     response.write rs(0) & "<br>"
                                     rs.movenext
                            Loop

                            conn.close
                            set conn = nothing
                            set rs = nothing
                       %>
            </body>
<html>
Table DDL

CREATE TABLE [dbo].[NewId] (
             [NewId] [int] NULL
             ) ON [PRIMARY]
GO

Sample Data used

1,2,3,4,5
blog comments powered by Disqus
ASP CODE ARTICLES

- ASP Forms
- ASP: The Beginning
- Getting Remote Files With ASP Continued
- Inbox and Outbox Manipulation in ASP
- Relational DropDownList Using VB.NET
- Ad Tracking URL Hits
- Use ViewState to display one record per page...
- Send Email using ASP.NET formatted in HTML
- ASP File Explorer
- ASP/XML Interview questions by Srivatsan Sri...
- Pressing RETURN won't submit the form
- This shows how you get the TEXT of a combo r...
- Group Data by Adrian Forbes
- Multiple checkbox select sample
- Multiple checkbox select with all values sam...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 10 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials