Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Random Integer ID Within a Range?

Author  Topic 

Blastrix
Posting Yak Master

208 Posts

Posted - 2004-01-16 : 15:53:42
I have a table with shopping cart orders, and I don't want users to be able to tell how many orders have been placed by comparing subsequent order numbers. I have two thoughts.

1) Generate a seed table of order numbers, maybe 10,000 or so at a time. For this to work it seems like there needs to be a process which generates more seed values when there are no seeds left, and then whenever a seed is taken, the record for that seed must be deleted.

2) Generate a random ID in my application, run a SELECT to see if the ID already exists, if not, then continue with the INSERT, otherwise go back to the beginning and try again.

Which one of these approaches do you guys think will perform better, or does anyone have any other suggestions on how to approach it?

Thanks,
Steve

setbasedisthetruepath
Used SQL Salesman

992 Posts

Posted - 2004-01-16 : 16:56:51
I would create order numbers based on a hash of current_timestamp

Jonathan
Gaming will never be the same
Go to Top of Page

Blastrix
Posting Yak Master

208 Posts

Posted - 2004-01-16 : 20:46:20
Do you mean like taking the timestamp and turning it into a format like YYYYMMDDhhmmss? If so, that number while not indicating the number or gap between multiple orders would be a bit too long I think. There's no(reasonable) way that there would ever be a need for more than 9 digits. Even if there were, I would still have another billion and some change left to go with an int.
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2004-01-17 : 18:15:24
Here's a variation that should fit in an int:

SELECT CAST(CAST(DateDiff(minute, 0, GetDate()) as varchar) +
Cast(DatePart(s, GetDate()) as varchar) +
Cast(DatePart(ms, GetDate()) as varchar) as int)


You can also play with CAST'ing a newid() to binary and hashing a substring of it into the result.
Go to Top of Page
   

- Advertisement -