How do I return random numbers as a column in SQL Server 2005?

后端 未结 12 790
误落风尘
误落风尘 2020-12-03 01:34

I\'m running a SQL query on SQL Server 2005, and in addition to 2 columns being queried from the database, I\'d also like to return 1 column of random numbers along with the

12条回答
  •  情歌与酒
    2020-12-03 01:55

    I use c# for dealing with random numbers. It's much cleaner. I have a function I use to return a list of random number and a unique key, then I just join the uniqueKey on the row number. Because I use c#, I can easily specify a range within which the random numbers must fall.

    Here are the steps to making the function: http://www.sqlwithcindy.com/2013/04/elegant-random-number-list-in-sql-server.html

    Here is what my query ends up looking like:

    SELECT 
       rowNumber, 
       name, 
       randomNumber
    FROM dbo.tvfRandomNumberList(1,10,100) 
    INNER JOIN (select ROW_NUMBER() over (order by int_id) as 'rowNumber', name from client        
                )as clients
    ON clients.rowNumber = uniqueKey
    

提交回复
热议问题