代码改变世界

Sql查询时加自动增长列

2010-01-22 19:08  三皮开发时  阅读(766)  评论(0编辑  收藏  举报

1:没有主键的情形:
   Select identity(int,1,1) as iid,* into #tmp from TableName
   Select * from #tmp
   Drop table #tmp
2:有主键的情形:
   Select (Select sum(1) from TableName where KeyField <= a.KeyField) as iid,* from TableName a order by iid