select *,identity(1,1) taoistong into #taoistong from 表 select * from #taoistong where taoistong=列的序号
--> 测试数据:[tb] if object_id('[tb]') is not null drop table [tb] go create table [tb]([编号] varchar(7),[时间] datetime,[数量] int) insert [tb] select '0100001','2009-09-28 13:18:00.000',18 union all select '0100002','2009-09-28 13:18:00.000',29 union all select '0100002','2009-09-28 13:45:00.000',15 union all select '0100003','2009-09-28 13:45:00.000',42 union all select '0100004','2009-09-28 13:45:00.000',11 union all select '0100001','2009-09-28 13:45:00.000',12 union all select '0100003','2009-09-29 10:45:00.000',18 union all select '0100004','2009-09-29 10:45:00.000',20 declare @colorder int set @colorder=3 --列的序号declare @name varchar(50) select @name= name from syscolumns where id=object_id('tb') and colorder=@colorder exec('select * from tb where '+@name+'=12')/*编号 时间 数量 ------- ----------------------- ----------- 0100001 2009-09-28 13:45:00.000 12(1 行受影响) */
select * from #taoistong where taoistong=列的序号
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([编号] varchar(7),[时间] datetime,[数量] int)
insert [tb]
select '0100001','2009-09-28 13:18:00.000',18 union all
select '0100002','2009-09-28 13:18:00.000',29 union all
select '0100002','2009-09-28 13:45:00.000',15 union all
select '0100003','2009-09-28 13:45:00.000',42 union all
select '0100004','2009-09-28 13:45:00.000',11 union all
select '0100001','2009-09-28 13:45:00.000',12 union all
select '0100003','2009-09-29 10:45:00.000',18 union all
select '0100004','2009-09-29 10:45:00.000',20
declare @colorder int
set @colorder=3 --列的序号declare @name varchar(50)
select @name= name from syscolumns where id=object_id('tb') and colorder=@colorder
exec('select * from tb where '+@name+'=12')/*编号 时间 数量
------- ----------------------- -----------
0100001 2009-09-28 13:45:00.000 12(1 行受影响)
*/