我有一张表格如下
ID Name Code
1 x y
5 c v
3 v h
10 k 4
23 m g
现在我想重新订购5000以下的号码
ID Name Code
5001 x y
5002 c v
5003 v h
5004 k 4
5005 m g
什么是最快的查询SQL Server来处理这个,因为该表包含超过百万行?
答案 0 :(得分:1)
试试这个解决方案
declare @a as int;
SET @a = 5000;
UPDATE tablename SET @a=id=@a+1;
答案 1 :(得分:0)
使用cursor更新表中的所有行
DECLARE @i int=5000
DECLARE cur cursor FAST_FORWARD READ_ONLY
FOR
SELECT id
FROM tbl
DECLARE @d varchar(50)
OPEN cur
FETCH NEXT FROM cur INTO @d
WHILE @@FETCH_STATUS=0
BEGIN
UPDATE dbo.tbl
SET id=@i+1
WHERE id=@d
FETCH NEXT FROM cur INTO @d
END
Close cur
DEALLOCATE cur
SELECT * FROM tbl
答案 2 :(得分:0)
使用此查询。
UPDATE A SET A.ID = 5000 + B.RowNUM
FROM TempTable AS A
INNER JOIN
(SELECT *, ROW_NUMBER() OVER(order by ID) AS RowNUM FROM TEMPTABLE) AS B
ON A.ID = B.ID
答案 3 :(得分:0)
执行此操作,将获得您想要的结果。
SET @i = 5000;
UPDATE table_name SET ID=(@i:=@i+1);