Reputation: 676
I have a query like this
Select * from Customers where CustId = 100
Is there a way to get a specific column name if I pass the column position in the select list.
So if Column 2 is FirstName, I should be able to get
Select *, column[2].Name from Customers Where CustID = 100
Upvotes: 0
Views: 77
Reputation: 9139
Still not sure I understand what you are doing, but maybe this sort of thing will help.
Select *
,(select c.name
from sys.columns c
inner join
sys.objects o on c.object_id=o.object_id
where o.name = 'Customers'
and column_id = 2) column_name
from Customers
where CustID = 100
Or if it easier witha variable:
declare @column_name SYSNAME
select @column_name = c.name
from sys.columns c
inner join
sys.objects o on c.object_id=o.object_id
where o.name = 'Customers'
and column_id = 2
select *
,@column_name
from Customers
where CustID = 100
Upvotes: 1