Oracle:找不到FROM关键字选择参数中的预期错误

时间:2016-02-04 09:33:18

标签: oracle

我的功能低于错误。

错误:FROM keyword not found where expected

这是我的功能:

    private int BauteilLieferzeit(string Materianummer)
    {
        try
        {
            OracleCommand cmd = new OracleCommand(
                " Select MATNR, AVG_DAUER" +
                " AVG " +
                    " (DATEDIFF " +
                        " (mi, Z.APL_ANFDATUM, " +
                        " Z.STA_LIEFERDATUM)) " +
                " as AVG_DAUER " +
                " from ZDATA AS Z " +
                " where MATNR = '" + Materianummer + "'" 
                        , OraVerbindung._conn);


            OracleDataReader r = cmd.ExecuteReader();

            if (r.HasRows)
            {
                int Restminuten = OraVerbindung.Lieferzeit;
                while (r.Read())
                {
                    Restminuten = r.GetInt32(1);
                }
                return Restminuten;
            }
            else
            {
                return OraVerbindung.Lieferzeit;
            }
        }
        catch
        {
            return OraVerbindung.Lieferzeit;
        }


    }

2 个答案:

答案 0 :(得分:1)

在Oracle中,这不是有效的语法

from ZDATA AS Z

使用

 from ZDATA Z

代替(删除“AS”)

另外考虑使用绑定变量而不是字符串连接:

 " where MATNR = '" + Materianummer + "'" 
  • 搜索“SQL注入”。

答案 1 :(得分:0)

使用它。包括Marmite强调的问题。但错误FROM keyword not found where expected可能是因为select语句中缺少逗号。

修改:删除AVG_DAUER列,因为它后来会派生。

private int BauteilLieferzeit(string Materianummer)
{
    try
    {
        OracleCommand cmd = new OracleCommand(
            " Select MATNR," +
            " AVG " +
                " (DATEDIFF " +
                    " (mi, Z.APL_ANFDATUM, " +
                    " Z.STA_LIEFERDATUM)) " +
            " as AVG_DAUER " +
            " from ZDATA Z " +
            " where MATNR = '" + Materianummer + "'" 
                    , OraVerbindung._conn);


        OracleDataReader r = cmd.ExecuteReader();

        if (r.HasRows)
        {
            int Restminuten = OraVerbindung.Lieferzeit;
            while (r.Read())
            {
                Restminuten = r.GetInt32(1);
            }
            return Restminuten;
        }
        else
        {
            return OraVerbindung.Lieferzeit;
        }
    }
    catch
    {
        return OraVerbindung.Lieferzeit;
    }


}