在 Oracle DB 中按顺序获取列名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32125585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get column names in order in Oracle DB
提问by user2665166
I am getting all the column names in a table using:
我使用以下方法获取表中的所有列名:
SELECT COLUMN_NAME
FROM ALL_TAB_COLUMNS
WHERE TABLE_NAME='<TABLE_NAME>'
AND OWNER = '<SCHEMA>'
I am calling this same sql statment on an identical table in a different database, but getting the results in a different order. Is there something I can append to my sql such that the columns will be returned in the same order?
我在不同数据库中的相同表上调用相同的 sql 语句,但以不同的顺序获取结果。有什么我可以附加到我的 sql 以便列将以相同的顺序返回的吗?
回答by sstan
Assuming your tables really are identical with columns defined in the same order in both databases, you can order by COLUMN_ID
to ensure consistent ordering.
假设您的表与在两个数据库中以相同顺序定义的列确实相同,您可以排序COLUMN_ID
以确保一致的排序。
SELECT COLUMN_NAME
FROM ALL_TAB_COLUMNS
WHERE TABLE_NAME='<TABLE_NAME>'
AND OWNER = '<SCHEMA>'
ORDER BY COLUMN_ID
回答by Jeffrey Kemp
If you want to guarantee that the columns are listed in the same order regardless of the order in which they were defined, sort by the column name:
如果要保证列以相同的顺序列出而不管它们的定义顺序如何,请按列名称排序:
...
ORDER BY column_name