111,093
社区成员




--楼主说的第二个列是指不知道列名的情况下
create table test(col1 int,col5 int,col6 int)
insert into test
select 1,2,3 union all
select 4,5,6 union all
select 7,8,9
go
create proc GetValueByPos
(
@tablename varchar(50),--表名
@hang int,--行号
@lie int --列号
)
as
begin
declare @liename varchar(50)
declare @sql varchar(8000)
;with m as
(
select name,lie=row_number() over (order by getdate())
from syscolumns where id=object_id(@tablename)
)
select @liename=name from m where lie=@lie
set @sql=
'select row_number() over (order by getdate()) as hang,* into #t from '+@tablename
+char(13)+'select '+@liename+' from #t where hang='+ltrim(@hang)
+char(13)+'drop table #t'
exec(@sql)
end
exec GetValueByPos 'test',2,3
/*
col6
-----------
6
*/
看是什么数据库吧,如果是oracle得话:Select 列名 From tablename Where rownum =2
如果是mysql的话,Select 列名 From tablename Limit 1,1
如果是sqlServer的话,需要嵌套下Top。