简体   繁体   中英

Error with SQL and Python - table or view does not exist

I am unable to run the scrip if I add left JOIN t2 ON Col10 = Col1 as a command in the sql query in Python. Without it I can run the script with no problem.

the error I get is: SQL Error: ORA-00942 table or view does not exist

PS- The script full script is working fine in SQL developer.

Any advise why?

import cx_Oracle
import pandas as pd

conn = cx_Oracle.connect('......')

cur = conn.cursor()

sql = "SELECT \
    Col1, \
    Col2 \
    FROM t1 \
    left JOIN t2 ON Col10 = Col1"

cur.execute(sql)

sql_data = pd.DataFrame(cur.fetchall())
col_names = [row[0] for row in cur.description]
sql_data.columns = col_names
sql_data

PS- The full script is working fine in SQL developer.

If that's the case, it means that user - you're connected to in SQL Developer - has access to both t1 and t2 tables.


If Oracle complains that

ORA-00942 table or view does not exist

when you run the same SELECT statement from Python, it means that user you're connected to now

conn = cx_Oracle.connect('......')

doesn't have the privilege to access the t2 table. That might be because

  • you misspelled its name
  • someone dropped the table so it doesn't exist any more in that schema
  • it is owned by someone else but you're accessing it as the owner granted you privileges to do so (and those privileges have now been revoked)
  • you use it as a public synonym which has been dropped

or some other reason. It is difficult to debug "generic" code you posted as, obviously, tables aren't really t1 nor t2 and columns aren't col1 nor col2 etc. Therefore, re-read what I've just said and try to find the culprit. Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM