SQL - Oracle Query To Fetch Column Names - Stack Overflow
SQL - Oracle Query To Fetch Column Names - Stack Overflow
Asked 11 years, 10 months ago Modified 4 years, 11 months ago Viewed 657k times
How do I change the above query in Oracle 11g database? I need to get columns names as a resultset for
table 'users' excluding certain columns, specifying a schema. Right now I have all tables in my new
tablespace, so do I specify tablespace name in place of schema name?
Also is there a generic HQL for this? In my new Oracle database (I am new to Oracle), I only have
tablespace name, so is that equivalent to schema name (logically?)
sql oracle
Share Improve this question Follow edited Aug 8, 2017 at 14:46 asked Jan 5, 2012 at 7:47
bakoyaro pri_dev
2,548 3 36 63 11.3k 15 72 122
The Oracle equivalent for information_schema.COLUMNS is USER_TAB_COLS for tables owned by the current
user, ALL_TAB_COLS or DBA_TAB_COLS for tables owned by all users.
199
Tablespace is not equivalent to a schema, neither do you have to provide the tablespace name.
Providing the schema/username would be of use if you want to query ALL_TAB_COLS or DBA_TAB_COLS for
columns OF tables owned by a specific user. in your case, I'd imagine the query would look something
like:
EDIT: Uppercased the table- and column names as these are typically uppercase in Oracle; they are only
lower- or mixed case if created with double quotes around them.
Share Improve this answer Follow edited Jun 6, 2018 at 14:46 answered Jan 5, 2012 at 8:08
David Faber Sathyajith Bhat
12.3k 2 31 40 21.4k 22 96 134
2 by the way I found a generic way of doing this irrespective of the databbase through jdbc .. with the link here :
kodejava.org/examples/163.html – pri_dev Jan 10, 2012 at 8:33
1 I also had to add and virtual_column = 'NO' to my query. – musicin3d Apr 21, 2015 at 22:38
Share Improve this answer Follow edited Nov 22, 2016 at 14:31 answered Dec 18, 2012 at 6:04
Karthik N G
2,151 1 19 20
32 Keep in mind that Oracle is case-sensitive. I typically use ...WHERE LOWER(Table_Name) = 'mytablename'; .
– user565869 Jan 23, 2015 at 19:58
3 @user565869 I'd use where lower(TABLE_NAME) = lower('WHATEVER') , else when the table name has some
uppercase character it won't find the table either – AlexanderD Jun 23, 2016 at 13:51
SELECT column_name FROM all_tab_cols WHERE UPPER(Table_Name) = UPPER('tablename'); works just as well.
– user3553260 Sep 24, 2016 at 2:38
Oracle stores object names in uppercase. Take advantage of that fact. If you need to "translate" the case, translate
the case of the user input - not the Oracle object name. I.e. WHERE table_name = UPPER('my_table_name') . If this
is part of a larger piece of code, you should uppercase the user input before passing it to the WHERE condition.
– osullic Apr 26, 2021 at 12:13
46 desc users
Share Improve this answer Follow answered Aug 10, 2013 at 22:01
Arsalan Sheikh
617 7 10
6 ... because although it represents an answer to 'how to get a list of all the columns of a table', it does not answer the
question that was asked: 1) it's not a query, 2) it doesn't limit/filter the columns returned, 3) is it returning a result
set? – Ehryk Sep 19, 2014 at 20:31
6 Lack of upvotes doesn't make desc users a bad answer to some questions, but it is not a good answer to this one.
– Ehryk Sep 19, 2014 at 20:31
and because its not a sql query, rather it's sql* plus command. checkout this for more info:
stackoverflow.com/questions/37133666/… – 01000001 Oct 15, 2018 at 18:22
You may try this : ( It works on 11g and it returns all column name from a table , here test_tbl is the table
name and user_tab_columns are user permitted table's columns )
8
select COLUMN_NAME from user_tab_columns
where table_name='test_tbl';
Share Improve this answer Follow answered Aug 27, 2015 at 4:22
Md. Salman Fahad
Famous
993 7 9
2 USER_TAB_COLUMNS is actually the columns of the tables owned by the user, not those of tables permitted to the
user. – David Faber Jun 6, 2018 at 14:48
the point is that in toad u have to write table name capital, like this:
6 select *
FROM all_tab_columns
where table_name like 'IDECLARATION';
Share Improve this answer Follow edited Dec 2, 2018 at 10:52 answered Dec 2, 2018 at 10:30
Tiago Martins Peres Ghadir Farzaneh
14.5k 18 88 150 449 5 6
Never heard of HQL for such queries. I assume it doesn't make sense for ORM implementations to deal
with it. ORM is an Object Relational Mapping, and what you're looking for is metadata mapping... You
wouldn't use HQL, rather use API methods for this purpose, or direct SQL. For instance, you can use JDBC
DatabaseMetaData.
I think tablespace has nothing to do with schema. AFAIK tablespaces are mainly used for logical internal
technical purposes which should bother DBAs. For more information regarding tablespaces, see Oracle
doc.
TABLE_NAME='"+_db+".users' will fail; you need to separate owner/schema and table name in ALL_TAB_COLUMNS
– David Faber Jun 6, 2018 at 14:49
The only way that I was able to get the column names was using the following query:
2 select COLUMN_NAME
FROM all_tab_columns atc
WHERE table_name like 'USERS'
On Several occasions, we would need comma separated list of all the columns from a table in a schema.
In such cases we can use this generic function which fetches the comma separated list as a string.
2
CREATE OR REPLACE FUNCTION cols(
p_schema_name IN VARCHAR2,
p_table_name IN VARCHAR2)
RETURN VARCHAR2
IS
v_string VARCHAR2(4000);
BEGIN
SELECT LISTAGG(COLUMN_NAME , ',' ) WITHIN GROUP (
ORDER BY ROWNUM )
INTO v_string
FROM ALL_TAB_COLUMNS
WHERE OWNER = p_schema_name
AND table_name = p_table_name;
RETURN v_string;
END;
/
So, simply calling the function from the query yields a row with all the columns.
EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE_NUMBER,HIRE_DATE,JOB_ID,SALARY,COMMISSION_PCT
Note: LISTAGG will fail if the combined length of all columns exceed 4000 characters which is rare. For
most cases , this will work.
Share Improve this answer Follow answered Oct 14, 2017 at 8:37
Kaushik Nayak
30.9k 6 32 45
1 SELECT
obj.object_name,
atc.column_name,
atc.data_type,
atc.data_length
FROM
all_tab_columns atc,
(SELECT
*
FROM
all_objects
WHERE
object_name like 'GL_JE%'
AND owner = 'GL'
AND object_type in ('TABLE','VIEW')
) obj
WHERE
atc.table_name = obj.object_name
ORDER BY
obj.object_name,
atc.column_name;
Share Improve this answer Follow edited Jul 2, 2015 at 18:40 answered Jul 2, 2015 at 17:43
Marshall Davis Dave C.
3,377 5 39 47 31 1
What if there are multiple tables with the same name, but different owners? – David Faber Jun 6, 2018 at 14:50
1. SELECT * FROM <SCHEMA_NAME.TABLE_NAME> WHERE ROWNUM = 0; --> Note that, this is Query Result, a
ResultSet. This is exportable to other formats. And, you can export the Query Result to Text format.
0 Export looks like below when I did SELECT * FROM SATURN.SPRIDEN WHERE ROWNUM = 0; :
Share Improve this answer Follow edited Jun 6, 2018 at 14:40 answered Jun 6, 2018 at 14:19
Uddhav P. Gautam
7,412 3 47 64
You can use the below query to get a list of table names which uses the specific column in DB2:
-1 SELECT TBNAME
FROM SYSIBM.SYSCOLUMNS
WHERE NAME LIKE '%COLUMN_NAME';
Note : Here replace the COLUMN_NAME with the column name that you are searching for.
Share Improve this answer Follow edited Jun 12, 2012 at 13:28 answered Jun 12, 2012 at 10:23
Jonathan Spooner Arulazagan Rathinam
7,682 2 34 41 Infosys
1
Perhaps you should look for DB2 questions to answer? I doubt someone searching for Oracle would need a DB2
answer. – RichardTheKiwi Oct 19, 2012 at 21:30
1 This answer actually helped me. I'm using DB2 in Oracle mode and it doesn't support all_tab_cols. – wm_eddie Aug
13, 2013 at 7:06
Share Improve this answer Follow answered Feb 26, 2018 at 21:51
Reza Rahimi
603 7 6
Highly active question. Earn 10 reputation (not counting the association bonus) in order to answer this question. The
reputation requirement helps protect this question from spam and non-answer activity.