今天修改系统系统时发现其有张表结构没有主健!!其他的字段都有重复的数据不能做主健 而新表又要引用此表的数据。
于是建了个字段ID想设成主键,但问题来了,里面已经有大量的数据了怎么添加这唯一的ID呢?
最后用游标解决了:
declare @i int
set @i=1
DECLARE authors_cursor CURSOR FOR SELECT * FROM TestTable
OPEN authors_cursor
FETCH NEXT FROM authors_cursor
update TestTable set id=@i where current of authors_cursor
WHILE @@FETCH_STATUS = 0
begin
FETCH NEXT FROM authors_cursor
set @i=@i+1
update CuttingFabric set id=@i where current of authors_cursor
end
close authors_cursor
deallocate authors_cursor
大家有什么更好的办法提下哦 !
2010-03-31