sql批量修改的问题
现在有个表,表中有10w条数据,有个人员编号字段,本来应该是从1到100000的,但是由于某些原因,现在编号字段只有从1到90000,还有一万的数据录入的时候没有录入人员编码,也就是说有1万的记录人员编码是空的,现在怎么给这1万的记录添加上人员编码呢,并且让编码从现在编码的最大开始向后排呢
表info如下
id p_code(人员编码)
[解决办法]
--初始化数据insert into tb values(1,'5');insert into tb values(2,'');insert into tb values(3,'6');insert into tb values(4,'');insert into tb values(5,'');insert into tb values(6,'4');insert into tb values(7,'3');--执行之前select * from tb--执行操作declare @count intset @count=1 --这里这个1表示人员编码的起始值while @count<=7 --这里这个7表示一共有多少条记录 begin if exists(select p_code from tb where p_code=@count) set @count=@count+1--如果表中p_code这个字段有该值那么自增加1 else update tb set p_code=@count where id=(select top 1 id from tb where p_code ='')--更新所有p_code字段没有值的记录的第一条 end--执行之后select * from tb