[解决]按列索引选择列,而不按列名选择列! [英] [Solved] SELECT columns by column-index NOT by columnname!

查看:127
本文介绍了[解决]按列索引选择列,而不按列名选择列!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过列索引而不是列名从表中选择一个或多个列?
通过使用此查询,我得到了表的columnIndices

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



更新:



UPDATE:
OP has posted his resolution as an answer.

推荐答案

列没有特定的顺序,因此按索引检索它们将是a)没有意义,并且b)无法预测.
Columns have no specific order, so retrieving them by index would be a) pointless, and b) unpredictable.

blockquote>


列索引供数据库内部使用.当然,您可以查询索引,但是它们的意义并不大.虽然在大多数情况下,它们的列索引将是创建列的顺序,但是如果您删除或插入列,它们可以更改.

在查询中使用列索引没有必要或没有用处,除非您确定它永远不会改变,并且您愿意承担后果并在改变时感到头疼.

通过使用列名可以使您更轻松地使用.
The column-index is for internal use for the database. Of course you can query the indexes, but they don''t mean much. While in most part they column index will be the order the columns created, but they can change if you delete or insert columns.

There is no need or usage in using the column-index in your query, unless you are certain it will NEVER change AND you are willing to take consequence and add headache whenever it changes.

Make your like easier by using column names.


过去,当我使用几种不能一起使用的产品/技术时,我不得不做这样的事情.我必须使用称为A B和C的列,但是解决方案可能相似.

您可以创建视图并在过程映射中将列名从名称重命名为数字.

因此,您可以执行以下操作:
I''ve had to do something like this in the past when working with a couple of products/technologies that didn''t play together. I had to use columns that were called A B and C, though, but the solution might be similar.

You can create a view and rename the column names in the process mapping from the name to a number.

So, you''d do something like:
select firstname as [1], lastname as [2] from mytable;



如果这样做,则可以按编号的列系统进行选择,但是我不确定这是否对您的特定情况有所帮助.

干杯.



If you do that, you''d be able to select by a numbered column system, but I''m not sure if this helps your particular situation.

Cheers.


这篇关于[解决]按列索引选择列,而不按列名选择列!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆