declare @t table(姓名 varchar(30),年龄 int,性别 varchar(10))
insert @t select '小张',12,'男'
union all select '小王',54,'女'
select identity(int,1,1) 序列号,* into # from @t
select * from #
drop table #
序列号 姓名 年龄 性别
----------- ------------------------------ ----------- ----------
1 小张 12 男
2 小王 54 女
(所影响的行数为 2 行)
楼主的列的大小标识可以不通过表变量实现
declare @ta([序列号(增加)] int identity(1,1),姓名 varchar(10) , 年龄 int, 性别 varchar(5))
insert @ta
select *from 表名
select * from @ta
通过临时表
select
[序列号(增加)]=identity(int,1,1),* into #
from 表名
查询
select * from #
SQLSERVER2000中没有行号.
在SQLSERVER2000中要实现"行号",可以这样试试:
1.将查询结果插入到临时表中,并生成自动增量标识列(即IDENTITY列).语法为:
select id = identity(int,1,1),* into #tmp from table
id就是#tmp表中的IDENTITY列.然后select * from #tmp.
2.根据查询结果某列中行之间的递增关系来生成序号.注意:此种方法必须保证该列不能有重复值.
----创建测试数据
declare @t table(dt datetime)
insert @t
select '2006-09-01' union all
select '2006-09-03' union all
select '2006-09-04' union all
select '2006-09-06' union all
select '2006-09-08'
---方法一
select id = identity(int,1,1),* into #tmp from @t
select * from #tmp
drop table #tmp
----方法二
select
id = (select count(*) from @t where dt <= a.dt),
dt
from @t a
/*结果
id dt
------------------------------------------
1 2006-09-01 00:00:00.000
2 2006-09-03 00:00:00.000
3 2006-09-04 00:00:00.000
4 2006-09-06 00:00:00.000
5 2006-09-08 00:00:00.000
*/