sql server - Insert column to make the primary key in T-SQL -


i have huge table (more million records) right contains following 2 columns: customername , amountbilled

i want add column, can call purchaseid, customername + purchaseid becomes unique combination , hence can create primary key.

say example, original data looks this:

customername amountbilled -------------------------     bill          $2 bill          $3.5 joe           $5 

i want new table this:

bill    1    $2 bill    2    $3.5 joe     1    $5 

with second column calculated in sql.

what correct sql statement this?

alter table tablename     add purchaseid int null go ;with cte (   select *, rn = row_number() on (partition customername order @@spid)   tablename ) update cte set purchaseid = rn go alter table tablename     alter column purchaseid int not null go 

Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -