all generated IDs must be unique.how?
any help would be appreciated.See the old posts:-
d8daab96ffd68fce/dccbf13c18513f64?q=unique+random+number+in+sql+server&rnum=2&hl=e
n#dccbf13c18513f64" target="_blank">http://groups.google.co.in/group/co...ccbf13c18513f64
If you just need the unique number then define a identitiy property.
CREATE TABLE test(i bigint identity(10000,1), k varchar(1)
insert into test (k) values('h')
This will start inserting the value from 10001
Thanks
Hari
SQL Server MVP
"wen" <wenming_hu2002@.hotmail.com> wrote in message
news:%23YYMC%23WaFHA.2756@.tk2msftngp13.phx.gbl...
> all generated IDs must be unique.how?
> any help would be appreciated.
>|||There are a couple of ways you could do this but they depend on the reason
why you need to do it. In short, you haven't supplied sufficient
information for us to assist you.
For instance, why not use an IDENTITY? Or, why not use a UUID?
How long do they need to be unique? How frequently will you need them? All
at once or only one at a time?
Sincerely,
Anthony Thomas
"wen" <wenming_hu2002@.hotmail.com> wrote in message
news:%23YYMC%23WaFHA.2756@.tk2msftngp13.phx.gbl...
all generated IDs must be unique.how?
any help would be appreciated.|||"Anthony Thomas" <ALThomas@.kc.rr.com> wrote in message
news:ezY3GWZaFHA.2076@.TK2MSFTNGP15.phx.gbl...
> There are a couple of ways you could do this but they depend on the reason
> why you need to do it. In short, you haven't supplied sufficient
> information for us to assist you.
> For instance, why not use an IDENTITY? Or, why not use a UUID?
i will use this id(bigint) to identify users, and users will use this uid to
login system. so uuid is not proper as user's id for logining the system.
it's too long!
> How long do they need to be unique? How frequently will you need them?
All
> at once or only one at a time?
>
i need it in this case:
once user fill his/her information and submit the information into the
server-side, the server will write his/her information into a table(called
"u_account", assuming it has field uid(bigint, that must be unique), name,
email,etc) of db, and at the same time, the record of the table u_account
will get a userid(bigint) as users' id(naturally, it must be unique)
it would be great if db will generate a random number(bigint, from 10,000 to
10,000,000) and unique(not the same as the existing number existing in the
table u_account).
how?
thank you.
> Sincerely,
>
> Anthony Thomas
>|||On Sun, 5 Jun 2005 15:35:29 +0800, wen wrote:
(snip)
>it would be great if db will generate a random number(bigint, from 10,000 t
o
>10,000,000) and unique(not the same as the existing number existing in the
>table u_account).
Hi wen,
Unless you expect to get so many users that the range of available
numbers will fill up for more than 10%, I'd say:
1. Use RAND() to get a random number in the desired range;
2. Use an IF EXISTS() query to check if that number is already in use;
3. Go back to step 1 if it is.
Not pretty, but it works, and it will be fast enough (provided some
index can be used for step 2), since more than 10 iterations will be
extremely rare.
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment