1: 自增列 类型为:int identity(1,1) 当然也可以是bigint,smallint
eg: create table tbName(id int identity(1,1),description varchar(20))
或在用企业管理器设计表字段时,将字段设为int,将标识设为是,其它用默认即可
2: 查询时加序号:
a:没有主键的情形:
Select identity(int,1,1) as iid,* into #tmp from TableName
Select * from #tmp
Drop table #tmp
b:有主键的情形:
Select (Select sum(1) from TableName where KeyField <= a.KeyField) as iid,* from TableName a
3:生成自增序列号的表
eg: 生成一列0-30的数
Select top 30 (select sum(1) from sysobjects where name<= a.name)-1 as id from sysobjects a
当然,可能sysobjects 中没有这么多条记录,比如只有100条,我需生成1-800的序列号
如下处理:
Select (Select sum(