how can I select one or more columns from a table by column-index and NOT by columnname?

I got the columnIndices of table by using this query

SELECT name, colid
FROM sys.syscolumns
WHERE (id =
(SELECT id
FROM sys.sysobjects
WHERE (name = 'table_name')))

I got my answered.
We can fetch records according to column index by using this query

Declare @WhichOne int;
Declare @Sql varchar(200);
Set @WhichOne = 2;
With cte As
(Select name, Row_Number() Over (Order By column_id) As rn
From sys.columns
Where Object_Name(object_id) = 'MyTable')
Select @Sql = 'Select ' + QuoteName(name) + ' From MyTable'
From cte
Where rn = @WhichOne;
Exec(@Sql);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.