Quantcast
Channel: Interactive.Octopus » SQL
Viewing all articles
Browse latest Browse all 2

Oracle / PLSQL: Getting a List of a Table’s Columns

$
0
0

Sometimes you need to know the column names for a specific table. I have needed this in the past when loading XML data into an empty table. For each node, I would check to see if a matching column named existed and if it didn’t I would update the table to add one, before loading the data. That way the query didn’t throw an error.

With Oracle PL/SQL, this is pretty simple. All you need to do is to get a list of the column names from the system table “USER_TAB_COLS”. There can be other versions of this table like “DBA_TAB_COLS” depending on how you set up your accounts and what you logged in as. But in general this all works the same. So to get a list of the columns you would do something like this:

SELECT column_name
FROM user_tab_cols
WHERE table_name = 'myTableName'
/

Play around with this and see what other kinds of uses you can come up with. Doing a “SELECT *” will show you that there are several other values in there like the column’s data type, data length, if its nullable, and several others.

:) Happy coding!


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images