Select tables matching condition from all tables - sql

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

Related

Retrieve schema name from view definition

In Snowflake, I retrieve different views with the following SQL query:
SELECT * FROM "myDatabase"."mySchema"."VIEWS"
That returns a table with these columns notably:
TABLE_ID
TABLE_NAME
TABLE_SCHEMA_ID
TABLE_SCHEMA
TABLE_CATALOG_ID
TABLE_CATALOG
TABLE_OWNER
VIEW_DEFINITION
For each VIEW_DEFINITION column entries, I am trying to extract all the strings <Schema_Name>.<View_Name> (or at least the <Schema_Name>).
Is it possible to do that with a SQL query (or by any other way)?
Edit
The table I obtain using the initial query is as follows:
TABLE_ID
TABLE_NAME
TABLE_SCHEMA_ID
TABLE_SCHEMA
TABLE_CATALOG_ID
TABLE_CATALOG
TABLE_OWNER
VIEW_DEFINITION
0001
MY_TABLE_NAME
99
MY_TABLE_SCHEMA
20
PMY_TABLE_CATALOG
MY_OWNER_VIEWS_ADMIN
…
where the VIEW_DEFINITION column contains queries like the one below:
"CREATE OR REPLACE VIEW My_Table_Schema_VIEWS.My_Table_Name AS
WITH STUDY_SITE_SCOPE AS (
SELECT
...
FROM (
SELECT
A.SUBJECT_NUMBER
, A.SUBJECT_STATUS
FROM <Schema_Name>.<View_Name_1> X
JOIN <Schema_Name>.<View_Name_2> Y
...
)
JOIN (
SELECT
...
FROM <Schema_Name>.<View_Name_3> X
JOIN <Schema_Name>.<View_Name_4> Y
...
)
..."
From this VIEW_DEFINITION I am trying to extract all the <Schema_Name>.<View_Name_XX> strings (or at least the <Schema_Name>).
I assume you want to get all base schemas your current view is built on top of.
To answer your question short: Yes, it is.
Maybe the following procedure is giving you an idea on how to solve it via SQL or a Stored Procedure:
Query the view definition
Search for all strings within the view definition that follow the "FROM" or "JOIN" clause
Extract them and probably check for the database name in front of the schema name
You can use information_schema.tables:
select t.*
from information_schema.tables t
where t.table_type = 'VIEW'

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

Query to find column not showing anything?

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

Vertica Dynamic Max Timestamp from all Tables in a Schema

System is HP VERTICA 7.1
I am trying to create a SQL query which will dynamically find all particular tables in a specific schema that have a Timestamp column named DWH_CREATE_TIMESTAMP from system tables. (I have completed this part successfully)
Then, pass this list of tables to an outer query or some kind of looping statement which will select the MAX(DWH_CREATE_TIMESTAMP) and TABLE_NAME from all the tables in the list (200+) and union all the results together into one list.
The expected output is a 2 column table with all said tables with that TS field and the max of each value. Tables are constantly being created and dropped, so the point is to make everything totally dynamic where no TABLE_NAME values are ever hard-coded.
Any idea of Vertica specific ways to accomplish this without UDF's would be greatly appreciated.
Inner Query (working):
select distinct(table_name)
from columns
where column_name = 'DWH_CREATE_TIMESTAMP'
and table_name in (select DISTINCT(table_name) from all_tables where schema_name = 'PTG_DWH')
Outer Query (attempted - not working):
SELECT Max(DWH_CREATE_DATE) from
WITH table_name AS (
select distinct(table_name)
from columns
where column_name = 'DWH_CREATE_DATE' and table_name in (select DISTINCT(table_name) from all_tables where schema_name = 'PTG_DWH'))
SELECT MAX(DWH_CREATE_DATE)
FROM table_name
Thanks!!!
No way to do that in one SQL .
You can used the below method for node max timestamp columns values
select projections.anchor_table_name,vs_ros.colname,max(max_value) from vs_ros,vs_ros_min_max_values,storage_containers,projections where vs_ros.colname ilike 'timestamp'
and vs_ros.salstorageid=storage_containers.sal_storage_id
and vs_ros_min_max_values.rosid=vs_ros.rosid
and storage_containers.projection_name=projections.projection_name
group by projections.anchor_table_name,vs_ros.colname

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.