日期:2014-05-18 浏览次数:20685 次
--初始化数据
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 int
set @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