【问题标题】:ORA-00936: missing expression | ASP .NET CORE 3.1 MVC | WITH FILES AS | NPocoORA-00936: 缺少表达式 | ASP .NET CORE 3.1 MVC |文件作为 | NPoco
【发布时间】:2021-07-20 22:13:13
【问题描述】:

我的函数抛出异常“ORA-00936: missing expression”时遇到问题,但查询在 Oracle SQL Developer 上运行...

有谁知道这可能来自哪里?

public List<(string, string, string, string)> GetSomething(string idWorkflow)
{
    var nb = new List<(string, string, string, string)>();

    if (!string.IsNullOrEmpty(idWorkflow))
    {
        try
        {
            List<(string, string, string, string)> result = _core.Query<dynamic>(
                $"WITH FILES AS " +
                $"(SELECT m.col1, m.col2 FROM TABLE1 m WHERE m.col5=@0 AND EXISTS(SELECT 1 FROM TABLE3 s WHERE s.col5 = m.col5 AND m.col6 = s.col8) AND m.col7 IS NULL) " +
                $"SELECT " +
                $"f.col1, " +
                $"f.col2, " +
                $"(SELECT col9 FROM TABLE2 h, TABLE4 d WHERE h.col1 = d.col10 AND d.col11 = f.col1 AND col12 = 3 AND d.col13 = 'INV' AND ROWNUM = 1) col3, " +
                $"(SELECT col9 FROM TABLE2 h, TABLE4 d WHERE h.col1 = d.col10 AND d.col11 = f.col1 AND col12 in (5, 6) AND d.col13 = 'INV' AND ROWNUM = 1) col4 " +
                $"FROM FILES f"
                ,
                idWorkflow
                ).Select(x => ((string)x.col1, (string)x.col2, (string)x.col3, (string)x.col4)).ToList();
            return result;
        }
        catch (Exception e)
        {
            var test = e.Message;
            return nb;
        }
    }

    return nb;
}

【问题讨论】:

  • ASP.NET 是一个 Web 应用程序框架,而不是一个数据访问库。该查询几乎肯定与您在 Oracle Developer 中尝试的查询相同。将整个查询存储在一个单独的变量中并检查它。顺便说一句,当这些字符串都不包含任何数据时,为什么要使用$?无论如何使用字符串插值来构造查询是非常危险的,并且很容易导致 SQL 注入或类型转换问题
  • @PanagiotisKanavos 你觉得这样更好吗?

标签: oracle npoco


【解决方案1】:

这是我解决问题的方法:

public List<(string, string, string, string)> GetSomething(string idWorkflow)
{
    var nb = new List<(string, string, string, string)>();
    var param = new { workflow = idWorkflow };
    var query = @";WITH FILES AS (
              SELECT m.col1, m.col2 FROM TABLE1 m WHERE m.col5=@workflow AND EXISTS(SELECT 1 FROM TABLE3 s WHERE s.col5 = m.col5 AND m.col6 = s.col8) AND m.col7 IS NULL)
                SELECT
                f.col1,
                f.col2,
                (SELECT col9 FROM TABLE2 h, TABLE4 d WHERE h.col1 = d.col10 AND d.col11 = f.col1 AND col12 = 3 AND d.col13 = 'INV' AND ROWNUM = 1) col3,
                (SELECT col9 FROM TABLE2 h, TABLE4 d WHERE h.col1 = d.col10 AND d.col11 = f.col1 AND col12 in (5, 6) AND d.col13 = 'INV' AND ROWNUM = 1) col4
                FROM FILES f";
    if (!string.IsNullOrEmpty(idWorkflow))
    {
        try
        {
            List<(string, string, string, string)> result = _core.Query<dynamic>(query, param).Select(x => ((string)x.col1, (string)x.col2, (string)x.col3, (string)x.col4)).ToList();
            return result;
        }
        catch (Exception e)
        {
            var test = e.Message;
            return nb;
        }
    }
    return nb;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-18
    • 2017-08-31
    • 1970-01-01
    相关资源
    最近更新 更多