本文介绍了从列索引而不是列名中检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
专家,
感谢您的关注.
我的桌子
hi expert,
I am grateful for your attention.
My Table
id name department salary
1 ABC 100000
2 ABD Accounts 76000
3 ADE Testing 59000
如果我给出部门的列索引
那我想变得像
if i give the column index of department
then I want to get like
department
----------
Accounts
Testing
问题将如何解决.
请帮忙.
预先感谢.
How the problem will be solve.
please help.
Thanks in advance.
推荐答案
declare @columnname as varchar(500)
declare @tablename as varchar(500)
declare @Columnindex as int
declare @query as nvarchar(max)
--table name
set @tablename='attendance2'
--column index
set @Columnindex=1
select @columnname= COLUMN_NAME from INFORMATION_SCHEMA.Columns
where ORDINAL_POSITION=@Columnindex and TABLE_NAME=@tablename
set @query='select ' + @columnname +' From '+@tablename
exec sp_executesql @query
select distinct [department] from [tablename]
select distinct(department) from tblname
这篇关于从列索引而不是列名中检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!