我有个表tableName,里面大概存放了10万数据,现在我想在这个表里面增加一列序号(xh),xh从1,2,3,4……10万编号
我采用的方式是,select row_number() over (order by jssj) as xh,jjdid into #aa from tableName
然后 update tableName set xh=b.xh from tableName a, #aa b where a.jjdid=b.jjdid
但是我发现这个jjdid有重复的,所以里面的序号xh也就有重复的,没有达到预期效果。
请教各位大侠了。大概实现如下效果1,2,3,4……10万,不打乱原来存储的顺序
------最佳解决方案--------------------
大概格式如下:
;
WITH cte
AS ( SELECT row_number() OVER ( ORDER BY GETDATE() ) AS xh ,
*
FROM tableName WITH ( NOLOCK )
)
UPDATE tableName
SET xx = xx
FROM tableName a
INNER JOIN cte b ON a.主键 = b.主键
------其他解决方案--------------------
那这个出来的不就是你要的咯?
SELECT row_number() OVER ( ORDER BY GETDATE() ) AS xh ,
*
FROM tableName WITH ( NOLOCK )
------其他解决方案--------------------
ORDER BY GETDATE() 这个东西一般是在没有什么列可以row_number时使用,由于getdate()即使同一时刻,都是自上而下,所以产生的ID是顺序的。
------其他解决方案--------------------
alter table tableName
add column ID int identity(1,1)
------其他解决方案--------------------
加一列,设为自增,然后
insert into 目标表(除自增外的字段)
select 除自增外的字段 from tableName
------其他解决方案--------------------
这个顺序是你想要的吗?
select row_number() over (order by getdate()) as xh,jjdid into #aa from tableName
------其他解决方案--------------------
这个并不是之后插入
这句执行之后,表就会多加一排序列了
------其他解决方案--------------------
SET IDENTITY_INSERT TableName OFF
那你可以,插入后取消自增
------其他解决方案--------------------
你用个cte临时存放嘛,然后update源表
------其他解决方案--------------------