0

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

1
Contributor
1
Reply
2
Views
6 Years
Discussion Span
Last Post by MARKAND911
0

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

Edited by MARKAND911: forget to write semicolon

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.