3

This code:

SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='xxx'

The above code throws an exception:

ORA-00900: invalid SQL statement

What did I do wrong? The above code worked long ago I could swear.

2
  • Is MyTable a column name in TABLES? Commented Nov 28, 2011 at 15:57
  • No, it was a typo... see updated text. Commented Nov 28, 2011 at 16:04

6 Answers 6

5

Oracle does not support INFORMATION_SCHEMA, you need to use ALL_TABLES, see here

Sign up to request clarification or add additional context in comments.

3 Comments

it's actually part of the ANSI standard and SQL server is not the only dbms to support it
thanks for the clarification vc 74, I just noticed it was part of the ANSI standard
Yep, just another standard oracle is reluctant to comply to... ;)
5

Assuming you want to check the schema you are currently connected to I would use user_tables :

SELECT table_name 
FROM USER_TABLES
WHERE table_name='xxx'

if you want to check the table is in in a different schema use all_tables don't forget to add the owner predicate as the table may exist is several schemas :

SELECT table_name 
FROM ALL_TABLES
WHERE table_name='xxx' AND owner='yourschemahere'

Comments

3

Have you migrated from another dbms?

AFAIK, Oracle does not support INFORMATION_SCHEMA (not even a subset of it) but you can retrieve a lot of metadata by querying the data dictionary.

Comments

2
SELECT
    TABLE_NAME
FROM
    ALL_TABLES
WHERE
    TABLE_NAME = 'YourTableName'

http://en.wikipedia.org/wiki/Oracle_metadata#Example_1:_finding_tables

Comments

0

On my most recent project requirements was to check if certain tables existed in an Oracle DB using C#.

Prerequisites:

Oracle .Net Assembly

App.Config file with connection string

I came up with this to meet this requirement.

private static void CheckIfOracleTableExists()
{
 try
   {
       string connectionString = ConfigurationManager.ConnectionStrings["dbConnectString"].ConnectionString;
       if (connectionString == null) throw new Exception();

       using (OracleConnection orclConn = new OracleConnection(connectionString))
       using (OracleCommand orclCmd = new OracleCommand())
           {
           orclConn.Open();
           orclCmd.Connection = orclConn;

           string commandText = String.Format("SELECT COUNT(*) FROM " + DbTestName);

           orclCmd.CommandText = commandText;
           orclCmd.CommandType = CommandType.Text;
           orclCmd.CommandTimeout = Convert.ToInt32(DbTimeout);

           try
           {
               orclCmd.ExecuteScalar();
               {
                   if (orclCmd.RowSize == 0) return;
                   TableExists = true;

                   orclConn.Close();
                   orclConn.Dispose();
                   orclCmd.Dispose();
               }
            }
            catch (OracleException oex)
            {
               if (oex.ToString().Contains("table or view does not exist"))
                  {
                     Console.WriteLine("\r\n\tTable not found.");
                  }
                  TableExists = false;
            }
            catch (OracleException oex)
            {
                Console.WriteLine("{0}", oex);
                TableExists = false;
            }
            catch (Exception ex)
            {
                if (ex.ToString().Contains("Object reference not set to an instance of an object"))

                Console.WriteLine("\r\n\t Invalid Connection String.");
            }
            TableExists = false;
           }
    }    
}//// / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /

2 Comments

I think that your answer would be more useful to folks if you were able to contract and explain your solution.
Setting a global variable and returning nothing? That is so ugly.
0

Actual code version of Kevin Burton's answer, with both the versions with and without schema:

    public Boolean TableExists(OracleConnection connection, String tableName)
    {
        return TableExists(connection, tableName, null)
    }

    public Boolean TableExists(OracleConnection connection, String tableName, String schema)
    {
        String sql;
        if (schema == null)
            sql = "SELECT TABLE_NAME FROM USER_TABLES WHERE TABLE_NAME=:table";
        else
            sql = "SELECT TABLE_NAME FROM ALL_TABLES WHERE TABLE_NAME=:table AND OWNER=:schema";
        OracleCommand command = new OracleCommand(sql, connection)
        command.Parameters.AddWithValue("table", tableName);
        if (schema != null)
            command.Parameters.AddWithValue("schema", schema);
        using (DbDataReader reader = command.ExecuteReader())
            return reader.HasRows;
    }

Comments

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.