2

I have a table with a integer primary key. The inserts to the table are made via a stored procedure.

I want to use random numbers for the primary key, for example:

ID
4345,
3432,
6686,
8556

The length of the number must be the same for each row.

I tried using identity specifications but it is incremental, not random.

3
  • 1
    Why do you want that? Commented Oct 13, 2013 at 5:08
  • 4
    ABS(CHECKSUM(NEWID()))? Commented Oct 13, 2013 at 5:38
  • SUBSTRING(CAST(ABS(CHECKSUM(NEWID())) AS VARCHAR),0,5) and retrying until the id doesnt exits. Thanks! Commented Oct 14, 2013 at 15:46

1 Answer 1

1

I can think of two options for that -

Get a random value and do retries - creating a random number using MYSQL

Use auto increment which produces unique values but the results are not random - How to set initial value and auto increment in MySQL?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.