How to read all table names from Oracle thru Spark - oracle

I want to fetch table names from Oracle database via Spark.
But it always failed.
with error:
Exception in thread "main" java.sql.SQLSyntaxErrorException:
ORA-00942: table or view does not exist
Sql = SELECT * FROM sys.tables WHERE 1=0, OriginalSql = SELECT * FROM
sys.tables WHERE 1=0, Error Msg = ORA-00942: table orview does not
exist
Have I missed something ? thanks.
class TrxxConnection(sparkSession:SparkSession) {
val trxxCon = sparkSession.read
.format("jdbc")
.option("url", "jdbc:oracle:thin:#//*****:****/****")
.option("user", "*****")
.option("password", "*****")
.option("driver", "oracle.jdbc.driver.OracleDriver")
// import sparkSession.implicits._
// // List the name of all tables in ARAMIS Database
def AllTableList =
trxxCon
.option("dbtable", "information_schema.tables") // or use sys.tables
.load()
.select("name").orderBy($"name".asc)
}
// below is unit test
println("Begin to establish connection to Oracle")
val AllTableList = new TrxxConnection(sparkSession).AllTableList // Dataset[Row] type
println("Connection to Oracle established")
println("Number of all tables: " + AllTableList.count())
AllTableList.show()

Apparently, there's no such thing as tables owned by sys available to you (nor me, when we're at it):
SQL> select count(*) from sys.tables;
select count(*) from sys.tables
*
ERROR at line 1:
ORA-00942: table or view does not exist
What to do? Use something that is available to you, e.g. your own tables in user_tables:
SQL> select count(*) from user_tables;
COUNT(*)
----------
30
or tables accessible to you, owned either by you or someone else:
SQL> select count(*) from all_tables;
COUNT(*)
----------
842
SQL>
or, if you are powerful enough, the whole database's tables by querying dba_tables:
SQL> connect sys#pdb1 as sysdba
Enter password:
Connected.
SQL> select count(*) from dba_tables;
COUNT(*)
----------
2398
SQL>
Pick one.
As I'm connected as sys right now, what do I see in that tables thing you used?
SQL> select * from tables;
select * from tables
*
ERROR at line 1:
ORA-00942: table or view does not exist
SQL>
Still nothing.
So, where exactly did you find information about tables owned by sys you could use for that query?

Related

In sql without insert values in table, how i can see column of table

SQL> create table justlike(customerid varchar(19),first_name varchar(40),last_name varchar(100),Address varchar(50),city varchar(30),pincode varchar(10),state varchar(20));
Just use:
desc <table_name>;
This will print the description of your table columns
in your case:
desc justlike;
You can always check the table definition, in case you are using Oracle, by running below query -
SELECT * FROM USER_TAB_COLS
WHERE TABLE_NAME = 'JUSTLIKE';
OR you can write a select on table itself -
SELECT * FROM JUSTLIKE;

Incorrect output while using dictionary tables inside Trigger

I am using ALL_TABLES/ALL_TAB_COLUMNS to get count of tables in my schema (EDW_SRC) and another schema(EDW_STG). I get correct counts when i run the query in my sql developer as shown below. But if i put the same query inside a trigger, i get wrong count for other schema(EDW_STG).
Please refer below code:
(This is just a sample code to replicate the issue, not my business requirement. I am referring ALL_TAB_COLUMNS in my actual code to get the number of columns in a particular table in different schema, for which i have Select access.)
select user from dual;
USER
-----
EDW_SRC
DROP TABLE ABC;
Table ABC dropped.
CREATE TABLE ABC(ID NUMBER);
Table ABC created.
select count(1) EDW_STG_CNT
from all_tables
where owner='EDW_STG';--Different Schema
EDW_STG_CNT
----------
101
select count(1) EDW_SRC_CNT
from all_tables
where owner='EDW_SRC';--My Schema
EDW_SRC_CNT
------------
1554
create or replace trigger trig_test_dml_abc
before insert on abc
DECLARE
V_STG_CNT number :=NULL;
V_SRC_CNT number :=NULL;
begin
DBMS_OUTPUT.PUT_LINE('***** TRIGGER OUTPUT *****');
select count(1) into V_SRC_CNT from all_tables
where owner='EDW_SRC'; --My Schema
DBMS_OUTPUT.PUT_LINE('My Schema EDW_SRC_CNT :'||V_SRC_CNT);
select count(1) into V_STG_CNT from all_tables
where owner='EDW_STG'; --Different Schema
DBMS_OUTPUT.PUT_LINE('Different Schema EDW_STG_CNT :'||V_STG_CNT);
end;
Trigger TRIG_TEST_DML_ABC compiled
INSERT INTO ABC VALUES (2);
1 row inserted.
***** TRIGGER OUTPUT *****
My Schema EDW_SRC_CNT :1554
Different Schema EDW_STG_CNT :2
The Different Schema count should be 101. Why is it coming as 2.
Oracle Version:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
Thanks
K

grant SELECT access to v$session to other users

I want to grant SELECT access to v$session to other users in an Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
but when I run this query:
SELECT owner, object_type FROM dba_objects WHERE object_name = 'V$SESSION';
I got this error:
00942. 00000 - "table or view does not exist"
Oracle v$ views are named V_$VIEWNAME and they have synonyms in format V$VIEWNAME and you can’t give privilege on a synonym. If you want to give permission to a V$ view you must give it like below
SQL> grant select on v_$session to hr;
We also needed a regular user without access to v$session to cleanup sessions. A function will execute with the privileges of the owning schema, so if you create a function as the user having access to V$SESSION you can execute it from the user not having the required privilege.
For example, IFH_OWNER has access to v$session, user id854812 doesn't:
As id854812:
select count(*) from v$session
ORA-00942: table or view does not exist
As IFH_OWNER:
select count(*) from v$session
56
create or replace function getSessionCount return int
as
vCnt int;
begin
select count(*) into vCnt from v$session;
return( vCnt);
end;
select getSessionCount from dual;
56
grant execute on getSessionCount to id854812;
As id854812:
select ifh_owner.getSessionCount from dual;
56

How to get the created / last DDL time for an Oracle synonym?

SQL Developer shows the creation and last DDL time for a public synonym in a table:
CREATED 15-AUG-09
LAST_DDL_TIME 15-AUG-09
OWNER PUBLIC
SYNONYM_NAME ISEMPTY
TABLE_OWNER MDSYS
TABLE_NAME OGC_ISEMPTY
DB_LINK (null)
How can I get the same information via a SQL query?
select * from all_synonyms where synonym_name = 'ISEMPTY'
does not get the created/last ddl dates.
More generally, is there a good way to see the queries that sql developer uses to display the data it displays (when you do not have access to a profiler)?
Thanks
You need the ALL_OBJECTS system view:
select *
from all_objects
where owner = 'OWNER_NAME'
and object_name = 'ISEMPTY'
and object_type = 'SYNONYM'

How to correctly make a public synonym

This is a pretty silly one, but I need help.
I have a table owned by mydbowner. It is named mydbowner.mytable. I tried to make a public synonym by issuing the command:
CREATE OR REPLACE PUBLIC SYNONYM mytable FOR mydbowner.mytable;
When I do this, and I query the table I get:
ORA-01775: looping chain of synonyms
How do I make this synonym without having the problem.
I think Justin is on the right track. What I think it actually means is that mydbowner.mytable doesn't exist.
Here's an example:
SQL> conn mbobak
Enter password:
Connected.
SQL> drop table mytable;
drop table mytable
*
ERROR at line 1:
ORA-00942: table or view does not exist
SQL> create public synonym mytable for mbobak.mytable;
Synonym created.
SQL> select * from mytable;
select * from mytable
*
ERROR at line 1:
ORA-01775: looping chain of synonyms
I think what's happening is that Oracle tries to resolve mytable, there is no mytable in mbobak schema, so it looks for it in PUBLIC, it finds it, and sees that it points to mbobak.mytable. But, mbobak.mytable doesn't exist, so, it looks for mytable in PUBLIC, and there's the loop.
And in fact, if you create mytable, the error goes away:
SQL> create table mytable as select * from dual;
Table created.
SQL> select * from mytable;
D
-
X
1 row selected.
SQL> drop table mytable;
Table dropped.
SQL> select * from mytable;
select * from mytable
*
ERROR at line 1:
ORA-01775: looping chain of synonyms
Yes, I realize that doesn't really entirely make sense, as, once the public synonym resolved to mbobak.mytable, and that's not found, it seems to me, it should return an error ORA-942 "table or view does not exist", which makes far more sense to me.
But, this does seem to be how it works.
QED
Hope that helps.
The error you're getting implies that mydbowner.mytable is not, in fact a table. What does
SELECT object_type
FROM all_objects
WHERE owner = 'MYDBOWNER'
AND object_name = 'MYTABLE'
return?

Resources