How to select specific column headers in Oracle Sql Developer - oracle

I am trying to select specific column headers from a view in oracle. It works when I have 1 column but if I add more column names I get back nothing.
I have tried add adding more AND column_name like '%%' but like I said earlier having more than 1 column name doesn't work. I tried replacing ALL_TAB_COLUMNS with USER_TAB_COLUMNS
SELECT column_name
FROM ALL_TAB_COLUMNS
WHERE TABLE_NAME = 'VW_CATALOG' AND column_name like '%SCBCRSE_SUBJ_CODE%'
Ideally I would like to have this work below:
SELECT column_name
FROM ALL_TAB_COLUMNS
WHERE TABLE_NAME = 'VW_CATALOG'
AND column_name like '%SCBCRSE_SUBJ_CODE%'
AND column_name like '%SCBCRSE_CRSE_NUMB%'
AND column_name like '%SCBCRSE_TITLE%'

'And' selects things where both the given conditions are true. It sounds like you're looking for 'Or'.
SELECT column_name
FROM ALL_TAB_COLUMNS
WHERE TABLE_NAME = 'VW_CATALOG'
AND (column_name like '%SCBCRSE_SUBJ_CODE%'
OR column_name like '%SCBCRSE_CRSE_NUMB%'
OR column_name like '%SCBCRSE_TITLE%'
)

Related

Find a certain column name in all views in one select statement

I'm searching for a certain column name in all views. I know that the following works for all tables but don't know how to do it for all views:
What I use to find a column_name='SHORT_TITLE' in all tables but now need to search all views:
SELECT table_name, column_name FROM all_tab_columns WHERE column_name='SHORT_TITLE'
Thanks
Oracle:
Joining all_tab_columns with all_views
SELECT table_name, column_name
FROM all_tab_columns, all_views
WHERE all_tab_columns.table_name = all_views.view_name
AND column_name = ...
Result of table_name will only be views
SQL:
We can do this by joining information_schema.views and information_schema.columns
SELECT v.table_name, column_name FROM information_schema.views v JOIN information_schema.columns c
ON v. = c.table_schema
AND v.table_name = c.table_name
WHERE column_name = ...
v.table_name is the name of the view.
EDIT: I added Oracle because I just realized the all_tab_columns in your question

What are the ways to write a sql script for getting mismatch between source and target datatype

Currently I am using
(select 'S',DATA_TYPE,DATA_LENGTH from all_tab_columns where table_name like '&Source_Table' and column_name='&Source_Column'
minus
select 'S',DATA_TYPE,DATA_LENGTH from all_tab_columns where table_name like '&Target_Table' and column_name='&Target_Column')
UNION ALL
(
select 'S',DATA_TYPE,DATA_LENGTH from all_tab_columns where table_name like '&Target_Table' and column_name='&Target_Column'
minus
select 'S',DATA_TYPE,DATA_LENGTH from all_tab_columns where table_name like '&Source_Table' and column_name='&Source_Column')
But above query need to run for each and every table. So i want to write a script which will use table and column names from a file/one time entry which will fetch mismatches between source and target.

How to search a column name within all tables of a database at specific schema

select table_name, column_name
from all_tab_columns
where column_name = '<YOUR_COLUMN_HERE_IN_CAPITAL_CASE>'
How can I use the above columns search query for a specific schema at Oracle DB?
I am asking because I have tried the query and it also returned many tables outside of the schema I am interested into, so they are of no interest to me.
select table_name, column_name
FROM all_tab_columns
where column_name = '<YOUR_COLUMN_HERE_IN_CAPITAL_CASE>'
and owner = '<schema>';
all_tab_columns contains all the columns on which the current user has privileges. So it may not return all the data.
dba_tab_columns contains information about all columns, but you may need some special privileges to query this dictionary view.
And finally, if you're interested only in the columns of all tables owned by the current user you can use:
select table_name, column_name
FROM user_tab_columns
where column_name = '<YOUR_COLUMN_HERE_IN_CAPITAL_CASE>';
But this view doesn't have an OWNER column (it only contains all the columns owned by the current user)
Use the following.
You can try this via SQL tool that is used by you
select table_name from all_tab_columns where column_name = 'PICK_COLUMN';
Or if you have DBA privileges,
select table_name from dba_tab_columns where column_name = 'PICK_COLUMN';
But if you are not sure about the column names you can add LIKE statements to current query.
Eg:
select table_name from all_tab_columns where column_name LIKE '%PICK_COLUMN%';
You can use the LIKE function to refine a search.
I don't know the exact names of the columns, but you could add the owner and the table name to this one:
select table_name, column_name
from all_tab_columns
where column_name = '<YOUR_COLUMN_HERE_IN_CAPITAL_CASE>'
in a way similar to this:
select column1,column2,column3,more_columns
from all_tables_you_need
where column1 like '%&column1%'
and column2 like '%&column2%'
and column3 like '%&column3%';
Either of those 3 variables can also be empty, but you can also put a very specific one in there, so that the result will vary a lot. Behold use of the UPPER function, if you are not sure of the case, or you want to ignore case sensitivity.

List out the table names having `clob` or `blob` or `lob` containing columns

I have around 1000 table out of which I need to list out the table names having clob or blob or lob containing columns. Is there any query to list out the same from my schema?
Try like this,
SELECT DISTINCT table_name
FROM user_tab_cols
WHERE data_Type IN ('CLOB', 'LOB', 'BLOB');
Try this one:
SELECT OWNER, TABLE_NAME, COLUMN_NAME, DATA_TYPE
FROM ALL_TAB_COLUMNS
WHERE DATA_TYPE IN ('CLOB', 'BLOB');
I havent a database handy, but this should work:
select * from ALL_TAB_COLUMNS a where a.DATA_TYPE in ('CLOB','BLOB','NCLOB','BFILE');
(see: http://docs.oracle.com/cd/B19306_01/server.102/b14237/statviews_2094.htm and http://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements001.htm)

Comparing two tables for different columns PL SQL

I very new to PL SQL and I have encountered a problem. Hopefully its not too hard to solve and I'm just going about it all wrong.
My problem is this: I have two tables with a different amount of columns. I need to run a check to see what the different columns are and then add them to one of the tables.
For example:
Table 1 has 1 column called name.
Table 2 has 2 columns called name and id.
(name has the same data type in both tables)
In this case, I would need to run a script that will check table 1 and 2, see that table 1 is missing the 'id' column and then add it to table 1.
Is this possible?
so far I have this:
SELECT TABLE_NAME, COLUMN_NAME FROM user_tab_columns WHERE table_name = 'TEST_TBL' OR TABLE_NAME ='TEST_TBL1'
which returns the columns for both tables. I have looked everywhere on the internet with no luck at all. I have tried to do intersect and join but with no luck.
If anyone has any help or could point me in the right direction I would appreciate it so much!
To get the different columns
SELECT TABLE_NAME, COLUMN_NAME FROM user_tab_columns
where table_name = 'Table1' AND COLUMN_NAME NOT IN ( SELECT COLUMN_NAME FROM USER_TAB_COLUMNS WHERE TABLE_NAME='table2')
UNION
SELECT TABLE_NAME, COLUMN_NAME FROM user_tab_columns
where table_name = 'table2' AND COLUMN_NAME NOT IN ( SELECT COLUMN_NAME FROM USER_TAB_COLUMNS WHERE TABLE_NAME='table1');

Resources