Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2002
    Location
    London
    Posts
    3

    Unanswered: Create temp table and creat rand column

    I need to create a temporary table from a result set and then add a column to place a random number in.

    The reason for this is to allow the result set to be initially displayed in a random order, so those records normally at the bottom have a chance to come near the top.

    Any ideas guys?

  2. #2
    Join Date
    Dec 2001
    Location
    Toronto, Canada
    Posts
    335
    Create your temporay table with data then use UPDATE and the RAND function

    Code:
    SELECT  col1
               , col2
               , col3
               , CAST(0 AS INT) AS Random
    INTO     #MYTBL
    FROM    masTable
    
    DECLARE @i int
    SET @i = 1000
    
    UPDATE  #MYTBL
    SET	Random = RAND(@i) * 100000,
    	@i = @i + 100
    
    SELECT *
    FROM   #MYTBL
    Read up on RAND and the seed value.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •