甲骨文说缺少,拼写错误或错放了FROM

时间:2013-03-15 20:47:49

标签: c# oracle11g odp.net

我第一次测试与Oracle的连接。连接本身似乎有效。然而...

  

从my_core.my_table选择TOP 1 ID

我使用的句子是

  

retVal = cmd1.ExecuteScalar()。ToString();

抛出

ORA-00923:找不到FROM关键字

   in Oracle.DataAccess.Client.OracleException.HandleErrorHelper(Int32 errCode,
OracleConnection conn, IntPtr opsErrCtx, OpoSqlValCtx* pOpoSqlValCtx, Object src
, String procedure, Boolean bCheck)
   in Oracle.DataAccess.Client.OracleException.HandleError(Int32 errCode, Oracle
Connection conn, String procedure, IntPtr opsErrCtx, OpoSqlValCtx* pOpoSqlValCtx
, Object src, Boolean bCheck)
   in Oracle.DataAccess.Client.OracleCommand.ExecuteReader(Boolean requery, Bool
ean fillRequest, CommandBehavior behavior)
   in Oracle.DataAccess.Client.OracleCommand.ExecuteReader()
   in Oracle.DataAccess.Client.OracleCommand.ExecuteScalar()
   in thiscs.Main() en C:\app\testuser\product\11.2.0\my\sample\thiscs.cs:line 44

以下是代码:

// C#

using System;
using Oracle.DataAccess.Client;
using System.Data;
using System.Data.Common;
using System.Transactions;

class thiscs
{
  static void Main()
  {
    string retVal = string.Empty;
    string providerName = "Oracle.DataAccess.Client";
    #region constr
    string constr =
       @"User Id=mytestuser;Password=mytestpass;Data Source=10.210.10.81:1521/my.svc.net;";

    #endregion

    // Get the provider factory.
    DbProviderFactory factory = DbProviderFactories.GetFactory(providerName);

    try
    {
      // Open a connection to the DB.
      DbConnection conn1 = factory.CreateConnection();
      conn1.ConnectionString = constr;
      conn1.Open();

      // Create a command to execute the sql statement.
      DbCommand cmd1 = factory.CreateCommand();
      cmd1.Connection = conn1;
      cmd1.CommandText = @"SELECT TOP 1 ID FROM my_core.my_table";

      // Execute the SQL statement to Select one rowfield in DB.
      retVal = cmd1.ExecuteScalar().ToString();
      Console.WriteLine("Field to be showed by cmd1: {0}", retVal);

      // close and dispose the connection
      conn1.Close();
      conn1.Dispose();
      cmd1.Dispose();
    }
    catch (Exception ex)
    {
      Console.WriteLine(ex.Message);
      Console.WriteLine(ex.StackTrace);
    }
  }
}

我也尝试了

  

SELECT TOP 1 [ID] FROM my_core.my_table

     

选择'嘿苹果,苹果,嘿'

两者都有相同的结果: ORA-00923:未找到FROM关键字

任何想法? TIA!

1 个答案:

答案 0 :(得分:8)

TOP 不是Oracle词汇表中的关键字,至少不是您希望它的上下文。

TOP 是Microsoft SQL Server使用的语法。

要复制此内容,请将SQL重写为:

SELECT ID FROM my_core.my_table WHERE rownum <= 1

换句话说,转换:

this: SELECT TOP N .... WHERE ...
                 |
                 +------------------------------+
                                                v
to:   SELECT       .... WHERE ... AND rownum <= N

另见:

另外请注意,您的示例中显示的查询相当荒谬。给定时间,索引使用情况,表格组织等将发生变化,这意味着除非您指定排序,否则您将检索哪个特定ID可能/将会更改。