檢查

2011-11-28 40 views
1

此代碼:檢查

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

上面的代碼拋出一個異常:

ORA-00900:無效的SQL語句

我做錯了什麼?上面的代碼早就工作了,我可以發誓。

+0

是MyTable的表A列的名字嗎? – Bert

+0

不,這是一個錯字...看到更新的文本。 – msfanboy

回答

5

Oracle不支持INFORMATION_SCHEMA,你需要使用ALL_TABLES,看到here

+1

它實際上是ANSI標準的一部分,SQL服務器不是唯一支持它的dbms –

+0

感謝澄清vc 74,我剛剛注意到它是ANSI標準的一部分 –

+0

是的,只是另一個標準oracle不願遵守。 ..;) –

3

你從其他DBMS遷移?

AFAIK,Oracle不支持INFORMATION_SCHEMA(甚至不支持它的子集),但是您可以通過查詢data dictionary來檢索大量元數據。

3

假設你要檢查當前連接到 模式我會用user_tables:在一個不同的模式

SELECT table_name 
FROM USER_TABLES 
WHERE table_name='xxx' 

,如果你想查詢的表在使用all_tables 不要忘記添加所有者謂詞,因爲表可能存在幾個模式:

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

我最近的項目需求是檢查某個表是否存在於使用C#的Oracle DB中。

先決條件:

甲骨文 .NET程序集

App.Config中文件,連接字符串

我想出了這個滿足這一要求。凱文·伯頓的回答

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; 
      } 
    }  
}////////////////////////////////////////// 
+1

我認爲,如果您能夠簽約並解釋您的解決方案,您的答案將會對人們更有用。 –

+0

設置全局變量並不返回任何內容?這太難看了。 – Nyerguds

0

實際的代碼版本,這兩個版本有和沒有的模式:

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; 
    }