1

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

从 my_core.my_table 中选择前 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);
    }
  }
}

我也试过

从 my_core.my_table 中选择前 1 个 [ID]

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

两者都具有相同的结果:ORA-00923: FROM 关键字未在预期的地方找到

有任何想法吗?蒂亚!

4

1 回答 1

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 可能/将会发生变化。

于 2013-03-15T20:50:17.267 回答