简体   繁体   English

SQL Server:不按名称选择列

[英]SQL Server : select columns not by name

Is it possible in SQL Server 2008 to select columns not by their names, but in the order as they appear in the table? 在SQL Server 2008中是否可以不按列名而是按表中出现的顺序选择列?

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. 原因是我想选择表的前5个6列,而不管内容是什么,因为它们的名称或列self可以更改或移动。

For the first 5 columns you can try this: 对于前5列,您可以尝试以下操作:

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. 编辑:更新的答案-旧的只选择前5行,而不是列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM