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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to generate unique id number 1

Status
Not open for further replies.

bookouri

IS-IT--Management
Joined
Feb 23, 2000
Messages
1,464
Location
US
I'm used to working with Oracle and am just recently being exposed to sql server.

I'm currently cleaning up an existing table that doesnt have a good primary key. In oracle I would have used something like "update mytable set new_id=1000+rownum"
to load the table with a unique col to use as a primary key. Can anybody point in in the right direction to accomplish this with t-sql?
 
Update table
set new_id=newid()
 
thanks, all the examples i found were using some kind of
select rank() over ( order by..... kind of construction.. to accomplish something similar..



 
Try
Code:
;with cte as (select *, Row_Number() over (order by (select null)) as RowNum from myTable)

update cte set NewID = RowNum

After that you can make your NewID to be Identity primary key, just make sure to set seed to be the max of current NewID.
 
wow, i dont understand that ";with cte as" construction but it works????

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top