select identity(int,1,1) as Num,a.*
into 新表名
from
(
select top 9999 * --top一定加入,因为在子查询中使用order by前面需要指定top
from byk
where 条件
order by 排序字段
) as a
原理:先将原始数据表进行排序,然后再插入identity.
如果写成如下格式:
select identity(int,1,1) as Num, *
into 新表名
from 数据表
where 条件
order by 排序字段
则sql会先对新表进行插入identity,然后再排序.