954,560 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

SELECT columns by column-index NOT by columnname!

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')))

MARKAND911
Junior Poster
126 posts since Nov 2008
Reputation Points: 10
Solved Threads: 2
 

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);
MARKAND911
Junior Poster
126 posts since Nov 2008
Reputation Points: 10
Solved Threads: 2
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: