Having trouble in getting the number of columns in a table in oracle using TOAD? - sql

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';

Related

Query to get columns from system.catalog table and do a select query

I am trying to build a query to fetch columns from the system.CATALOG table and to continue querying based on the resultset. I looked at a few queries but seem to be unable to find anything that satisfies my requirements. I don't have much to show, that I have tried as I don't know, how to approach this.
I am using Apache Phoenix DB. (Any SQL is also OK, as I am interested in learning.)
I have now written the query below, which will fetch me all the column names, that start with A in schema test for table element.
SELECT
COLUMN_NAME
FROM SYSTEM.CATALOG
WHERE TABLE_SCHEM = 'TEST'
AND TABLE_NAME = 'ELEMENT'
AND COLUMN_NAME LIKE 'A%'";
Now I want to use the list of columns names in an UPSERT query from the resultset of above query, to update these columns in the element table's records. So I am stuck here.
try this, it works perfect.
SELECT column_name
FROM system.catalog
WHERE table_name = 'your_table' AND key_seq IS NOT NULL
Example: To get the salt buckets on the table :
select table_name, salt_buckets
from SYSTEM.CATALOG
where salt_buckets is not null and table_name='TABLE_NAME';

Need to select all tables from a database in SQL Server where the most recent date-timestamp is from a year or more ago

I'm going through all tables in a database trying to determine which tables are old (have not been altered in a long time). I've been going through and flagging all tables with old DTS's as "old"
Is there a more efficient way to do this? Can I run a statement that scans all tables in a database for date-timestamp fields and then looks at the most recent ones?
Thank you in advance for any help you can provide!
You can use the INFORMATION_SCHEMA.COLUMNS view to retrieve all the tables having the timestamp column (assuming the column name is known and not used for other columns).
Use these to generate Dynamic SQL of the form
SELECT COUNT(*) ,#TableName FROM #TableName WHERE #TimeStampColumn < #TimestampToCheck
The tables where count(*) is 0 are the ones you need to look at
I would do something like this:
Show a COUNT(*) of rows written in the last year... if you get 0, you know the table isn't in use.
Use the information_schema to get the columns/tables.
SELECT 'SELECT COUNT(*) AS ['+TABLE_NAME+'] FROM ['+TABLE_CATALOG+'].['+TABLE_SCHEMA+'].['+TABLE_NAME+'] WHERE '+COLUMN_NAME+' > DATEADD(YY,-1,CONVERT(DATETIME,FLOOR(CONVERT(FLOAT,GETDATE()))))'
-- SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE DATA_TYPE='DATETIME'
--AND (COLUMN_DEFAULT='(getdate())' OR COLUMN_DEFAULT='CURRENT_TIMESTAMP')
You can add the last line in, if you only want columns with a default value.
Then take the output, copy to a new window, and run it!

count of fields with particular data type in a table

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'
;

Column count in oracle10g

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';

SQL/JDBC : select query on variable tablenames

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;`