用游标来实现:
declare @temp varchar(20),@i int,@utemp varchar(20)
set @temp='172.22.8.'
set @i=1
DECLARE abc CURSOR FOR
SELECT username FROM test ;
OPEN abc
FETCH next from abc into @utemp
WHILE @@FETCH_STATUS = 0
BEGIN
UPDATE dbo.test
SET ip = @temp+cast(@i as varchar(20))
FROM dbo.test
WHERE CURRENT OF abc;
set @i=@i+1
FETCH NEXT FROM abc
End
CLOSE abc
DEALLOCATE abc
用添加标识列实现:
Step1: ALTER TABLE test ADD index_c int IDENTITY(1,1)
Step2:
UPDATE test SET ip = '172.22.8.' + cast( index_c as varchar)
ALTER TABLE test DROP COLUMN index_c
select * from test