Query to find column not showing anything? - sql

I'm trying to find a table in a schema with a specific column name. So I used the following script, but it doesn't return anything:
select a.table_name, column_name,DATA_TYPE,DATA_LENGTH
from all_tab_columns a,USER_ALL_TABLES u
where a.TABLE_NAME=u.TABLE_NAME
and column_name like '%LATLONG%'
order by DATA_LENGTH desc;
On the other hand, a SELECT of table LATLONG_DETAIL will display a column called LATLONG_TYPE.
So why isn't the query displaying this table in its result?
All these queries are being run in the schema where table LATLONG_DETAIL resides.
Thanks.

You say you own the table LATLONG_DETAIL. The only other thing I can think of why your query isn't returning anything is that the column name is not in upper case. Does this query return anything?
SELECT a.table_name, column_name,DATA_TYPE,DATA_LENGTH
FROM all_tables u JOIN all_tab_columns a
ON u.table_name = a.table_name
AND u.owner = a.owner
WHERE UPPER(column_name) LIKE '%LATLONG%';

Related

Select tables matching condition from all tables

I want to select for all tables with columns that contain the word deposit from a list of all tables when the owner is like bi. How can I implement this?
For example, I can select all tables and owner:
SELECT * FROM ALL_TABLES WHERE OWNER LIKE '%BI';
But I want to then select all tables from table_name and run a query to find those columns that are type character and contain the string deposit. This I cannot understand how to do.
UPDATE:
The following query worked:
SELECT *
FROM ALL_TAB_COLUMNS
WHERE OWNER LIKE '%BI'
AND DATA_TYPE
LIKE '%NUMBER'
AND COLUMN_NAME
LIKE '%DEPOSIT%';
Just change the data_type to varchar% (to catch varchar and varchar2):
SELECT DISTINCT TABLE_NAME
FROM DBA_TAB_COLUMNS TBLS
WHERE UPPER(TBLS.OWNER) LIKE '%BI'
AND UPPER(COLUMN_NAME) LIKE '%DEPOSIT%'
AND UPPER(DATA_TYPE) LIKE 'VARCHAR%';

Adding filters to SQL query (ORACLE database)

I need to list tables, columns and their data type; I used this code:
select TABLE_NAME, COLUMN_NAME, DATA_TYPE
from dba_tab_columns
order by TABLE_NAME;
The problem is that the database is too large and I only need 2000 specific table/column/datatype. Knowing that a table with the same name and column can be present in multiple SCHEMA
For example (I have 2000 lines like that table names are very random and have nothing in common so is the case for the column names):
TABLE Column SCHEMA
------------------------------------------
DMT_AAAAAAA C1111 ANT_A1
DMT_AAAAAAA C1111 ANT_A2
BBBBBBBB A4444 ANT_A3
JHD6365 H5525 ZUGRU
WRK679 C3020 MUSTSU
TDG5378 C66739 SHGUY
I tried to filter by schema using this query :
select TABLE_NAME, COLUMN_NAME, DATA_TYPE
from dba_tab_columns
where OWNER in ('ANT_A1', 'ANT_A2', 'ANT_A1', 'ZUGRU', 'MUSTSU', 'SHGUY')
order by TABLE_NAME;
The issue now is that it lists all columns even the ones I don't need.
Is there a way to filter out only the lines needed?
Expected output :
|table name | column | data type|
DMT_AAAAAAA C1111 NUMBER
DMT_AAAAAAA C1111 VARCHAR
BBBBBBBB A4444 NUMBER
JHD6365 H5525 VARCHAR
WRK679 C3020 VARCHAR
TDG5378 C66739 VARCHAR
Thank you.
If you want to show the information about certain columns across schemas and tables, specify them in an IN clause using tuples:
select owner, table_name, column_name, data_type
from dba_tab_columns
where (owner, table_name, column_name) in
(
('DMT_AAAAAAA', 'C1111', 'ANT_A1'),
('DMT_AAAAAAA', 'C1111', 'ANT_A2'),
('BBBBBBBB', 'A4444', 'ANT_A3'),
('JHD6365', 'H5525', 'ZUGRU'),
('WRK679', 'C3020', 'MUSTSU'),
('TDG5378', 'C66739', 'SHGUY')
)
order by owner, table_name, column_name;
Check whether the following is enough. (I'm considering your output only)
select Distinct TABLE_NAME, COLUMN_NAME, DATA_TYPE from dba_tab_columns
If you don't want the tables, 'BBBBBBBB' and 'WRK679'
Put those in to a table (say tblExclude) and you can use the following
select Distinct TABLE_NAME, COLUMN_NAME, DATA_TYPE from dba_tab_columns a
Left Join tblExclude b on a.TABLE_NAME = b.TABLE_NAME
Where b.TABLE_NAME is null

Converting one to many relation into a json column in PostgreSQL

I'm trying to query two information_schema tables in PostgreSQL - tables and columns in order to get the following result:
table_name - columns_as_json_array
Sort of converting this one to many relation into a json array column.
I tried a lot of different methods and came up with somethings like this:
SELECT t.table_name, c.json_columns
FROM information_schema.TABLES t
LEFT JOIN LATERAL(
SELECT table_name, json_agg(row_to_json(tbc)) AS json_columns
FROM information_schema.COLUMNS tbc
WHERE t.table_name = tbc.table_name
GROUP BY table_name
) as c ON TRUE;
This results a list of table_names but the json_columns always contains all of the columns available instead of the columns of that certain table.
Any ideas?
I don't really see the point for a lateral join here. As far as concerns, you can get the expected results by aggregating information_schema.columns:
select table_name, json_agg(row_to_json(c)) json_columns
from information_schema.columns c
group by table_name
order by table_name

How to determine the number of "Column Name" in a table?

I have a table tblEmployeeInfowhich has atleast a 100+ column name.
I want to know how many column name are in that table. Is that possible?
NOTE:
tbleEmployeeInfo has no data inside yet.
I would recommend using the INFORMATION_SCHEMA views. You can see all the columns and their types by doing:
select c.*
from INFORMATION_SCHEMA.COLUMNS c
where table_name = 'tbleEmployeeInfo';
(You might want to include the table_schema as well.)
To get the count, just use COUNT(*):
select count(*)
from INFORMATION_SCHEMA.COLUMNS c
where table_name = 'tbleEmployeeInfo';
SELECT COUNT(*)
FROM sys.columns
WHERE object_id = object_id('tblEmployeeInfo')

Find table_name that contains two known column names

I want to find the tables that contain both of the two columns together in one table. I tried this:
SELECT COLUMN_NAME, TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME in ('CurrencyName', 'CurrencyKey');
This query generates all the tables that has either one of the CurrencyName or CurrencyKey column.
But I want the table that has both of these columns together.
Please shoot some ideas.
Thanks!
You are close. You want to use group by and then validate that you have two matches using a having clause:
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME in ('CurrencyName', 'CurrencyKey')
GROUP BY TABLE_NAME
HAVING COUNT(*) = 2;
Note: To be sure you have the right table, you should use TABLE_SCHEMA as well in the query.