Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

create a unique table column

Status
Not open for further replies.

nicatt

Programmer
Apr 2, 2001
43
US
I wish to create a unique column in a sybase table that currently contains 7 million rows. I also want future row inserts to have similar uniqueness when inserted using a cursor in a stored proc. The table will need to have a column added to it.

It was suggested that the problem could be resolved by creating a unique column with a (datetime + random number) value. This should create a unique value for each record as the cursor inserts it. This presents a two fold problem. I don't seem able to add a random number to a datetime and converting the datetime to an integer does not appear to be permitted. Additionally, this method will not resolve how to create unique numbers for the 7 million records already in the table.

I could try the 'identity_column' but that can often lead to some really strange values.

Any ideas?
 
I think I found how to do it. Run a loop against the table incrementing a integer in the new column. Then in the stored proc, check the table for the MAX(integer value) in the table's column at the start of the proc. (If there are 113367 records the max value is 113367 the first time). Then use 113367 + 1 as the starting number for the next cursor insert. Each subsequent insert will be the previous number + 1.

Cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top