How to update a table with auto-increment value ?


update usercode field in userinfo table with auto increment value:


Step 1:
 
SELECT usercode1 = 0,* into #tmp_user
FROM userinfo

DECLARE @counter int
SET @counter = 0 

UPDATE
#tmp_user 
 SET @counter = usercode1 = @counter + 1


select a.
usercode1,a.UserId
into #tbl2
from
#tmp_user a
inner join
userinfo b on a.UserId=b.UserId


UPDATE
    a
SET
   a.
usercode=b.usercode1  
FROM
   
userinfoa
    INNER JOIN #tbl2 b
       ON a.
UserId = b.UserId
 
drop table #
tmp_user 
drop table #tbl2



or Step 2: Easier way to solve this problem

DECLARE @i As varchar(150)
SET @i = 0
UPDATE
userinfo
SET @i= usercode = @i+1

Post a Comment

0 Comments