从列索引而不是列名中检索数据 [英] retrieve data from a column by column index not columnname

查看:66
本文介绍了从列索引而不是列名中检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

专家,

感谢您的关注.

我的桌子

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.

推荐答案

这将起作用.

This will work.

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]


嗨....


U可以使用以下查询.


Hi....


U can use below query.


select distinct(department) from tblname


这篇关于从列索引而不是列名中检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆