Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Create random number
Message
From
03/06/2012 12:55:42
 
General information
Forum:
Microsoft SQL Server
Category:
Other
Environment versions
SQL Server:
SQL Server 2005
Miscellaneous
Thread ID:
01545107
Message ID:
01545190
Views:
62
>>>Hi,
>>>
>>>In VFP I am creating a random number (that has to fit into N(8,0) size field) as following:
>>>
>>>
>>>rand(-1)
>>>nNumber = int( rand() * 100000000 )
>>>
>>>
>>>I found online article that has the following code for creating a random number:
>>>
>>>
>>>SELECT CAST(RAND() * 1000000 AS INT) AS [RandomNumber]
>>>
>>>
>>>Is this equivalent to the VFP code?
>>
>>Unless you are running a lottery or something, I see no reason why you would want a random number. Generating of any random number implies the new number might already exist in the table.
>>
>>I also think it really unfortunate that you started by asking for a random number, and no one stops you to ask what do you intend to do with it. That resulted in a lot of noise that served no purpose.
>>
>>You seem to be requiring a number that is unique to each record. If not, please explain what you are doing.
>
>Yes, I do need a unique number to be set into a field of a table (not PK field). In VFP I have used the random number approach for this case and have not had any problems. Why could not the same work in SQL?

As others have pointed out a random number is not neccessarily unique - whether generated by VFP or T-SQL. To ensure uniqueness you will need to check that it has not already been used and, if it has, try another randomly generated number. Something like this example:
http://sqlhut.wordpress.com/2012/02/20/unique-fixed-length-number-in-sql-server/
Previous
Reply
Map
View

Click here to load this message in the networking platform