I am working with oracle10g. How can I determine the number of columns in a relation specified as a SQL query?
You need to take a look at DBMS_SQL package. A select statement can include n columns, thus you need to parse it manually using PL/SQL.
DBMS_SQL.REC_TAB structure will give you plenty of information about your select statement.
http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14258/d_sql.htm
You can dba_tab_columns synonym.
SELECT COUNT(COLUMN_NAME) from DBA_TAB_COLUMNS
WHERE TABLE_NAME='NAME_HERE_RELATION';
Related
I have so far figured out that to describe a table I can use the below:
select dbms_metadata.get_ddl('TABLE','<my table name>','<table owner>') from dual;
I also found that I can get a list of tables from the current user using the below statement:
select table_name from user_tables;
However I need to find a way to combine these two so I get a (preferably SQL file) output which basically describes all the tables in the current schema. How can I go about that?
Call dbms_metadata in your query on user_tables:
select dbms_metadata.get_ddl('TABLE',table_name,user)
from user_tables;
When I look at a view/table, I can get the column names using the following SQL query:
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='table'
Obviously, this will work to show columns for tables; but it does not work for functions. Is there an easy way (besides getting one record from a function, and manually parsing it) to get the column names returned by a function?
Does this help?
SELECT *
FROM INFORMATION_SCHEMA.ROUTINE_COLUMNS
I'm unsure if this works for inline TVFs
I have used the following query to get count of columns with a particular datatype from a table. But I am not getting correct count, Its lot more than what I expect. What could be wrong with this query?
select count(t.data_type)
from all_tab_columns t
where t.TABLE_NAME='REG_ENG_FILES' and t.DATA_TYPE='VARCHAR2'
Is it possible to get count from metadata?
Reference
What Turophile says is that you can have schema1, schema2 having both a table REG_ENG_FILES. To be more specific, you can use:
select count(t.data_type)
from all_tab_columns t
where t.TABLE_NAME='REG_ENG_FILES'
and t.OWNER='MYSCHEMA'
and t.DATA_TYPE='VARCHAR2'
;
I am trying to get number of columns in a table using the following query:
select count(*)
from user_tab_columns
where table_name='MYTABLE';
but the results are shown as zero, even I had many columns in the table.
Could some one help me where I went wrong ?
You are using the wrong system table.
Try this.
select count(*) from ALL_TAB_COLUMNS where table_name='MYTABLE';
I'm using Oracle DB and I would like to write a SQL query that I could then call with JDBC. I'm not very familiar with SQL so if someone can help me, that could be great ! Here is the problem. I have a table MY_TABLE wich contains a list of another tables, and I would like to keep only the nonempty tables and those that their names start by a particular string.
The query I wrote is the following :
select TABLE_NAME
from MY_TABLE
where TABLE_NAME like '%myString%'
and (select count(*) from TABLE_NAME where rownum=1)<>0
order by TABLE_NAME;`
The problem comes from the second SELECT, but I don't know how can I do to use the TABLE_NAME value.
Does someone have an idea ?
Thanks.
[Added from comments]
Actually, I need to test the V$ views contained in the ALL_CATALOG table. But if I can find another table where all these views are contained too and with a NUM_ROWS column too, it would be perfect !
Standard versions of SQL do not allow you to replace 'structural elements' of the query, such as table name or column name, with variable values or place-holders.
There are a few ways to approach this.
Generate a separate SQL statement for each table name listed in MY_TABLE, and execute each in turn. Brute force, but effective.
Interrogate the system catalog directly.
Investigate whether there are JDBC metadata operations that allow you to find out about the number of rows in a table without being tied to the system catalog of the specific DBMS you are using.
Can you use oracle view USER_TABLES? then query will be much easier
select TABLE_NAME
from USER_TABLES
where TABLE_NAME like '%myString%'
and Num_ROWS > 0
order by TABLE_NAME;`