SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Generating Random Numbers on Every Row with RAND()
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Rock_query
Yak Posting Veteran

52 Posts

Posted - 05/01/2013 :  17:26:16  Show Profile  Reply with Quote
I am using the RAND() function. I am multiplying by 20 to get a larger number since RAND returns numbers between 0 and 1.

The problem is that RAND generates only 1 random number and copies that down each row. But I want a different randome number on each row.

I am using the AdventureWorks2012 database on SQL Server Express on my laptop.


DECLARE @Drago TABLE (
ProductID int IDENTITY (1,1),
Price money NOT NULL,
Qty int NULL
)


INSERT INTO @Drago (
Price,
Qty
)

SELECT ListPrice, RAND()*20
FROM Production.Product

SELECT *
FROM @Drago


Lamprey
Flowing Fount of Yak Knowledge

4614 Posts

Posted - 05/01/2013 :  17:43:10  Show Profile  Reply with Quote
Don't use the RAND function. Try using the NEWID function.

http://stackoverflow.com/questions/2247554/random-number-on-sql-without-using-newid
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 05/02/2013 :  04:36:10  Show Profile  Reply with Quote
http://beyondrelational.com/modules/2/blogs/70/posts/19431/rand-function-reset-values-for-each-group.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000