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

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

Related

How to select specific column headers in Oracle Sql Developer

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

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.

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

populating select list with common column names b/w two tabels via subquery -oracle

My code is clear enough to describe what i am trying to achieve
SELECT (select column_name from user_tab_cols where table_name='tbl1'
intersect
select column_name from user_tab_cols where table_name='tbl2')
FROM tbl2;
It is throwing ORA-01427: single-row subquery returns more than one row
I understand this error but don't know the alternate solution to achieve my goal .
You don't need the subquery here. Try this instead:
SELECT column_name FROM user_tab_cols WHERE table_name = 'tbl1'
INTERSECT SELECT column_name FROM user_tab_cols WHERE table_name = 'tbl2'
This will return a list of column names that exist in both tbl1 and tbl2.

How to get column info from oracle table you don't own (without using describe)?

How would one get columns information on table which he doesn't own, but has select granted? This is, without using DESCRIBE table_name. Consider this example:
// user bob owns table STUDENTS
grant select on students to josh;
// now josh logs in, normally he would do
describe bob.students;
// but he's looking for something along the lines
select column_name from user_tab_columns where table_name = 'STUDENTS';
// which doesn't work, as josh doesn't own any tables on his own
Any ideas? Is this even doable?
select column_name from all_tab_columns where table_name = 'STUDENTS';
edit: or, even better
select owner, column_name from all_tab_columns where table_name = 'STUDENTS';
Have a look on oracle data dictionary, it should help.
CONN HR/HR#HSD;
GRANT SELECT ON EMPLOYEES TO SCOTT;
CONN SCOTT/TIGER#HSD;
SELECT owner, column_name FROM all_tab_columns WHERE table_name = 'EMPLOYEES';

Resources