Reputation: 607
Is it possible in SQL Server 2008 to select columns not by their names, but in the order as they appear in the table?
The reason is that i want to select the first 5 oder 6 columns of a table, no matter what's the content, because it is possible that their names or the columns self can be changed or moved.
Upvotes: 0
Views: 222
Reputation: 834
For the first 5 columns you can try this:
select column_name,ordinal_position
from information_schema.columns
where table_schema = ...
and table_name = ...
and ordinal_position <= 5
Hope this works now. Solution found here. Edit: Updated answer - old one only selected first 5 rows, not columns.
Upvotes: 3