Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get column names in order in Oracle DB

Tags:

sql

oracle

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?

like image 741
user2665166 Avatar asked Aug 20 '15 18:08

user2665166


People also ask

How do I get a list of column names in PL SQL?

Best AnswerSELECT owner, column_nameFROM all_tab_columnsWHERE table_name = 'YOUR_TABLE_HERE'ORDER BY owner, table_name; You may wnat to add "AND owner =..." as the above query will return all tables/views that have the table_name 'YOUR_TABLE_HERE'. i.e. more than one owner can have a table/view of the same name.

How do I get a list of column names in a database?

SELECT * FROM INFORMATION_SCHEMA. COLUMNS WHERE COLUMN_NAME LIKE 'PREFIX%' ORDER BY TABLE_NAME; Query: SELECT * FROM INFORMATION_SCHEMA.

Can I reorder columns in Oracle?

You can reorder the columns on a page. To reorder columns: From the menu at the top of the page, select View, and then Reorder Columns.


2 Answers

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.

SELECT COLUMN_NAME 
FROM ALL_TAB_COLUMNS 
WHERE TABLE_NAME='<TABLE_NAME>' 
AND OWNER = '<SCHEMA>'
ORDER BY COLUMN_ID
like image 171
sstan Avatar answered Sep 28 '22 02:09

sstan


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
like image 43
Jeffrey Kemp Avatar answered Sep 28 '22 02:09

Jeffrey Kemp