I have a mySQL query to get columns from a table like this:
String sqlStr="select column_name
from information_schema.COLUMNS
where table_name='users'
and table_schema='"+_db+"'
and column_name not in ('password','version','id')"
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?)
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. Export looks like below when I did SELECT * FROM SATURN.SPRIDEN WHERE ROWNUM = 0;
:
"SPRTELE_PIDM" "SPRTELE_SEQNO" "SPRTELE_TELE_CODE" "SPRTELE_ACTIVITY_DATE" "SPRTELE_PHONE_AREA" "SPRTELE_PHONE_NUMBER" "SPRTELE_PHONE_EXT" "SPRTELE_STATUS_IND" "SPRTELE_ATYP_CODE" "SPRTELE_ADDR_SEQNO" "SPRTELE_PRIMARY_IND" "SPRTELE_UNLIST_IND" "SPRTELE_COMMENT" "SPRTELE_INTL_ACCESS" "SPRTELE_DATA_ORIGIN" "SPRTELE_USER_ID" "SPRTELE_CTRY_CODE_PHONE" "SPRTELE_SURROGATE_ID" "SPRTELE_VERSION" "SPRTELE_VPDI_CODE"
DESCRIBE <TABLE_NAME>
--> Note: This is script output.
You can use the below query to get a list of table names which uses the specific column in DB2:
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.
The only way that I was able to get the column names was using the following query:
select COLUMN_NAME
FROM all_tab_columns atc
WHERE table_name like 'USERS'
I find this one useful in Oracle:
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;
The query to use with Oracle is:
String sqlStr="select COLUMN_NAME from ALL_TAB_COLUMNS where TABLE_NAME='"+_db+".users' and COLUMN_NAME not in ('password','version','id')"
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.
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.
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.
select cols('HR','EMPLOYEES') FROM DUAL;
EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE_NUMBER,HIRE_DATE,JOB_ID,SALARY,COMMISSION_PCT,MANAGER_ID,DEPARTMENT_ID
Note: LISTAGG
will fail if the combined length of all columns exceed 4000
characters which is rare. For most cases , this will work.
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 )
select COLUMN_NAME from user_tab_columns
where table_name='test_tbl';
in oracle you can use
desc users
to display all columns containing in users table
The below query worked for me in Oracle database.
select COLUMN_NAME from ALL_TAB_COLUMNS where TABLE_NAME='MyTableName';
You can try this:
describe 'Table Name'
It will return all column names and data types
the point is that in toad u have to write table name capital, like this:
select *
FROM all_tab_columns
where table_name like 'IDECLARATION';
Source: Stackoverflow.com