Select from multiple tables oracle - oracle

I'm new in Oracle. I have a table that lists tablenames of database. Its name is "AD_Table". I want to select ID table, and createdby from ad_table list. For example in ad_table it has one column name tablename that represents table name in database:
tablename
---------
AD_Tab1
AD_Tab2
AD_Tab3
AD_Tab4
AD_Tab5
AD_Tab6
AD_Tab7
AD_Tab8
AD_Tab9
AD_Tab10
I want query like this :
SELECT
createdby
from (SELECT TABLENAME FROM AD_TABLE)
but it won't work. Can anyone help?

In Oracle you can have many tables with the same name, on different schemas;
assuming that you need to find all the tables, and their owners, whose names are contained in your table, you can try with something like this:
select owner, table_name
from AD_table AD
inner join dba_tables DBA ON ( dba.table_name = UPPER(ad.tableName))
Notice that you need to log in with a user having rights to make a select on DBA_TABLES to run this query.

Related

Using multiple select statement inside insert statement in Hive

I'm new in Hive. I have three tables like this:
table1:
id;value
1;val1
2;val2
3;val3
table2
num;desc;refVal
1;desc;0
2;descd;0
3;desc;0
I want to create a new table3 that contains:
num;desc;refVal
1;desc;3
2;descd;3
3;desc;3
Where num and desc are columns from table2 and refVal is the max value of column id in table1
Can someone guide me to solve this?
First, you have to create an table to hold this.
CREATE TABLE my_new_table;
After that, you have to insert into this table, as showed here
INSERT INTO TABLE my_new_table
[PARTITION (partcol1=val1, partcol2=val2 ...)]
select_statement1;
In the select_statement1 you can use the same select you would normally use to join and select the columns you need.
For more informations, you can check here

Find which oracle database object inserts data into a particular table?

I want to find the name of the stored procedure, package, function or trigger which is inserting data into a particular table, is there any inbuilt object so that i can use it. I am searching the name of the object from last 2 hours. Kindly help.
You cannot find the inserted PL/SQL block, but you can try out to identify the procedures/functions referring it. And increase your chances of finding.
select
proc_syn.nam,
proc_syn.referenced_owner,
proc_syn.referenced_name,
proc_syn.referenced_type,
syn_tab.table_name
from
dba_dependencies proc_syn, dba_synonyms syn_tab, dba_tables tables
where
REFERENCED_TYPE in ( 'SYNONYM','TABLE')
AND proc_syn.referenced_name = syn_tab.synonym_name
AND syn_tab.synonym_name = tables.table_name
AND syn_tab.owner = 'PUBLIC'
AND REFERENCED_NAME = 'YOUR_TABLE_NAME'
order by
proc_syn.referenced_owner, syn_tab.table_name;
The above query would return the table objects, that refer this table.
Note, this would return, when you use STATIC queries only. Any dynamic queries, is completely out of hand.
You can try by analyzing all the source of your DB with something like this:
select *
from dba_source
where upper(text) like '%TABLE_NAME%'
The pro of this approach is that you will get even dynamic code; the con is that you'll have even select, update, ...
If the table is statically referenced you can check the ALL_DEPENDENCIES view
SELECT * FROM ALL_DEPENDENCIES WHERE REFERENCED_NAME = '<your table>';
This will find all usages, not only insert
You can also search within source code:
SELECT * FROM ALL_SOURCE WHERE UPPER(TEXT) LIKE '%INSERT%<your table>%'
This will find the usage only if INSERT and your table are on the same line.
However if the command is built more dynamically then you will not find it that simply.
You can use user_source or user_dependencies table as below.
Using table user_source
select * from user_source
where upper(text) like '%<YOUR TABLE NAME in UPPER CASE>%'
You can select from user_source , all_source , dba_source depending on user you have logged in and permission that user have.
Using table user_dependencies
SELECT NAME FROM user_dependencies WHERE referenced_name = '<YOUR TABLE NAME in UPPER CASE>'
UNION
SELECT referenced_name FROM user_dependencies WHERE name = '<YOUR TABLE NAME in UPPER CASE>'
You can select from user_dependencies , all_dependencies , dba_dependencies depending on user you have logged in and permission that user have.

Oracle SQL Query for listing all Schemas in a DB

I wanted to delete some unused schemas on our oracle DB.
How can I query for all schema names ?
Using sqlplus
sqlplus / as sysdba
run:
SELECT *
FROM dba_users
Should you only want the usernames do the following:
SELECT username
FROM dba_users
Most likely, you want
SELECT username
FROM dba_users
That will show you all the users in the system (and thus all the potential schemas). If your definition of "schema" allows for a schema to be empty, that's what you want. However, there can be a semantic distinction where people only want to call something a schema if it actually owns at least one object so that the hundreds of user accounts that will never own any objects are excluded. In that case
SELECT username
FROM dba_users u
WHERE EXISTS (
SELECT 1
FROM dba_objects o
WHERE o.owner = u.username )
Assuming that whoever created the schemas was sensible about assigning default tablespaces and assuming that you are not interested in schemas that Oracle has delivered, you can filter out those schemas by adding predicates on the default_tablespace, i.e.
SELECT username
FROM dba_users
WHERE default_tablespace not in ('SYSTEM','SYSAUX')
or
SELECT username
FROM dba_users u
WHERE EXISTS (
SELECT 1
FROM dba_objects o
WHERE o.owner = u.username )
AND default_tablespace not in ('SYSTEM','SYSAUX')
It is not terribly uncommon to come across a system where someone has incorrectly given a non-system user a default_tablespace of SYSTEM, though, so be certain that the assumptions hold before trying to filter out the Oracle-delivered schemas this way.
SELECT username FROM all_users ORDER BY username;
select distinct owner
from dba_segments
where owner in (select username from dba_users where default_tablespace not in ('SYSTEM','SYSAUX'));
Below sql lists all the schema in oracle that are created after installation
ORACLE_MAINTAINED='N' is the filter. This column is new in 12c.
select distinct username,ORACLE_MAINTAINED from dba_users where ORACLE_MAINTAINED='N';
How about :
SQL> select * from all_users;
it will return list of all users/schemas, their ID's and date created in DB :
USERNAME USER_ID CREATED
------------------------------ ---------- ---------
SCHEMA1 120 09-SEP-15
SCHEMA2 119 09-SEP-15
SCHEMA3 118 09-SEP-15
Either of the following SQL will return all schema in Oracle DB.
select owner FROM all_tables group by owner;
select distinct owner FROM all_tables;

How to get all columns in Oracle in SYNONYMS

I know how to get all columns in oracle.
select * from all_tab_columns
but how can I get all columns from SYNONYMSas well?
Is this possible to do in oracle?
Isn't that a bit redundant? If you can see the table a synonym points to, then selecting from all_tab_columns gets you what you want.
You can get any synonyms for tables you can see thusly:
SELECT atc.*, s.synonym_name
FROM all_tab_columns atc LEFT JOIN all_synonyms s
ON (atc.owner = s.table_owner AND atc.table_name = s.table_name)
ORDER BY atc.owner, atc.table_name;

How can I find the OWNER of an object in Oracle?

I want to find the foreign keys of a table but there may be more than one user / schema with a table with the same name. How can I find the one that the currently logged user is seeing? Is there a function that gives its owner? What if there are public synonyms?
You can query the ALL_OBJECTS view:
select owner
, object_name
, object_type
from ALL_OBJECTS
where object_name = 'FOO'
To find synonyms:
select *
from ALL_SYNONYMS
where synonym_name = 'FOO'
Just to clarify, if a user user's SQL statement references an object name with no schema qualification (e.g. 'FOO'), Oracle FIRST checks the user's schema for an object of that name (including synonyms in that user's schema). If Oracle can't resolve the reference from the user's schema, Oracle then checks for a public synonym.
If you are looking specifically for constraints on a particular table_name:
select c.*
from all_constraints c
where c.table_name = 'FOO'
union all
select cs.*
from all_constraints cs
join all_synonyms s
on (s.table_name = cs.table_name
and s.table_owner = cs.owner
and s.synonym_name = 'FOO'
)
HTH
-- addendum:
If your user is granted access to the DBA_ views (e.g. if your user has been granted SELECT_CATALOG_ROLE), you can substitute 'DBA_' in place of 'ALL_' in the preceding SQL examples. The ALL_x views only show objects which you have been granted privileges. The DBA_x views will show all database objects, whether you have privileges on them or not.
I found this question as the top result while Googling how to find the owner of a table in Oracle, so I thought that I would contribute a table specific answer for others' convenience.
To find the owner of a specific table in an Oracle DB, use the following query:
select owner from ALL_TABLES where TABLE_NAME ='<MY-TABLE-NAME>';
Interesting question - I don't think there's any Oracle function that does this (almost like a "which" command in Unix), but you can get the resolution order for the name by:
select * from
(
select object_name objname, object_type, 'my object' details, 1 resolveOrder
from user_objects
where object_type not like 'SYNONYM'
union all
select synonym_name obj , 'my synonym', table_owner||'.'||table_name, 2 resolveOrder
from user_synonyms
union all
select synonym_name obj , 'public synonym', table_owner||'.'||table_name, 3 resolveOrder
from all_synonyms where owner = 'PUBLIC'
)
where objname like upper('&objOfInterest')
Oracle views like ALL_TABLES and ALL_CONSTRAINTS have an owner column, which you can use to restrict your query. There are also variants of these tables beginning with USER instead of ALL, which only list objects which can be accessed by the current user.
One of these views should help to solve your problem. They always worked fine for me for similar problems.
To find the name of the current user within an Oracle session, use the USER function.
Note that the owner of the constraint, the owner of the table containing the foreign key, and the owner of the referenced table may all be different. It sounds like it’s the table owner you’re interested in, in which case this should be close to what you want:
select Constraint_Name
from All_Constraints
where Table_Name = 'WHICHEVER_TABLE'
and Constraint_Type = 'R' and Owner = User;
It is like #entpnerd said, but I suggest you to use upper() clause:
select *
from ALL_TABLES
where upper(TABLE_NAME) = upper('<table_name>')

Resources