1

I am using jdbc to execute some sql queries. I have the following sql

sql="select distinct name, value  from myTable1  union select distinct name, value from myTable2";

Problem: sometimes one of these tables doesn't exist and I get java error.

Error message: ORA-00942:table or view doesn't exist.

How to adjust the query to run table only if it exists.

9
  • You have to do in the other order. First check if the table exists, then create the query that includes the table if existing. Commented Jan 17, 2019 at 8:23
  • 1
    Since it's only a select, why do you even care? If he doesn't find the table, I guess an exception is thrown which you can read. But simply catch the exception and return null? I really don't get why you bother here. Commented Jan 17, 2019 at 8:23
  • 1
    First check if the table exists, these question was already asked a lot of times. How can I detect a SQL table's existence in Java? Commented Jan 17, 2019 at 8:24
  • @maio290: i wouldnt like the select to break up only because one of the table is missing. It should get values from existing tables Commented Jan 17, 2019 at 8:25
  • 1
    Even if the two tables are found your query will fail because UNION should have same number/datatype of columns selected from both tables. Commented Jan 17, 2019 at 9:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.