Entity SQL 日期时间语法错误

发布于 2024-10-18 08:30:09 字数 1647 浏览 2 评论 0原文

有人知道我的语法有什么问题吗?我正在进行动态 eSQL 查询,并在尝试为 DateTime 数据类型设置 where 条件时遇到错误。这是错误:

查询语法无效。近期“2011”,第 1 行,第 135 列。

如果重要的话,我的实体中的 DateTime 类型实际上可以为 null DateTime?

但是,我认为这是我读过的所有内容中的正确语法。

这是代码:

List<EntityFilter<FirstRead>> filters = new List<EntityFilter<FirstRead>>()
        {
            new EntityFilter<StudyFirstRead> { PropertyName = "username", OpType = ExpressionType.Equal, Value = "cwoodhouse" },
            new EntityFilter<StudyFirstRead> { PropertyName = "FirstRead", OpType = ExpressionType.LessThan, Value = "DATETIME'2011-02-01 00:00'" }
        };

EntityFilter 所在位置:

  public class EntityFilter<T>
{
    public string PropertyName { get; set; }
    public ExpressionType OpType { get; set; }
    public object Value { get; set; }

我正在构建动态查询,如下所示:

StringBuilder builder = new StringBuilder();

        int counter = 0;

        string baseStr = @"SELECT VALUE val FROM " + contextName + "." + tableName + " AS val WHERE val.";

        builder.Append(baseStr); 

        foreach (EntityFilter<T> filter in filters)
        {
            //builder.Append(filter.PropertyName + " " + filter.OpTypeString() + " @p" + counter);
            builder.Append(filter.PropertyName + " " + filter.OpTypeString() + "'" + filter.Value + "'"); 
            counter++;

            if (counter < filters.Count)
                builder.Append(" AND val."); 
            else
            {
                break; 
            }
        }

        return builder.ToString(); 

Anyone know What is wrong with my syntax here? I am making dynamic eSQL Queries and running into an error when trying to make where condition for DateTime data type. This is the error:

The query syntax is not valid. Near term '2011', line 1, column 135.

If it matters the DateTime type in my entity is actually nullable DateTime?

However, I thought this is the correct syntax from everything I've read.

Here is the code:

List<EntityFilter<FirstRead>> filters = new List<EntityFilter<FirstRead>>()
        {
            new EntityFilter<StudyFirstRead> { PropertyName = "username", OpType = ExpressionType.Equal, Value = "cwoodhouse" },
            new EntityFilter<StudyFirstRead> { PropertyName = "FirstRead", OpType = ExpressionType.LessThan, Value = "DATETIME'2011-02-01 00:00'" }
        };

Where EntityFilter is:

  public class EntityFilter<T>
{
    public string PropertyName { get; set; }
    public ExpressionType OpType { get; set; }
    public object Value { get; set; }

And I am building dynamic queries like so:

StringBuilder builder = new StringBuilder();

        int counter = 0;

        string baseStr = @"SELECT VALUE val FROM " + contextName + "." + tableName + " AS val WHERE val.";

        builder.Append(baseStr); 

        foreach (EntityFilter<T> filter in filters)
        {
            //builder.Append(filter.PropertyName + " " + filter.OpTypeString() + " @p" + counter);
            builder.Append(filter.PropertyName + " " + filter.OpTypeString() + "'" + filter.Value + "'"); 
            counter++;

            if (counter < filters.Count)
                builder.Append(" AND val."); 
            else
            {
                break; 
            }
        }

        return builder.ToString(); 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤芳又自赏 2024-10-25 08:30:09

它实际上是实体 SQL 的正确语法(与常规 SQL 或 T-SQL 不同)。

事实证明,问题是单引号太多,因为我在 EntityFilter 对象和构建动态查询的方法中都有它们。

It actually is the correct syntax for entity SQL (different than regular SQL or T-SQL).

Turns out the problem was too many single quotes, because I had them in both the EntityFilter object and the method that built the dynamic query.

夏雨凉 2024-10-25 08:30:09

根据您的代码,SQL语句的末尾会产生类似

AND val.FirstRead = 'DATETIME'2011-02-01 00:00''

执行时您会收到错误

错误 102:“2011”附近的语法不正确。

显然这不是语法上正确的 SQL,快速修复应该是让您的过滤器集合如下所示:

List<EntityFilter<FirstRead>> filters = new List<EntityFilter<FirstRead>>() {
    new EntityFilter<StudyFirstRead> {
        PropertyName = "username",
        OpType = ExpressionType.Equal,
        Value = "cwoodhouse"
    }, new EntityFilter<StudyFirstRead> {
        PropertyName = "FirstRead",
        OpType = ExpressionType.LessThan,
        Value = "2011-02-01 00:00"
    }
};

according to you code the end of your SQL statement would produce something like

AND val.FirstRead = 'DATETIME'2011-02-01 00:00''

when executed you will get error

Error 102: Incorrect syntax near '2011'.

obviously that is not syntactically correct SQL, the quick fix whould be to have your filters collection as such:

List<EntityFilter<FirstRead>> filters = new List<EntityFilter<FirstRead>>() {
    new EntityFilter<StudyFirstRead> {
        PropertyName = "username",
        OpType = ExpressionType.Equal,
        Value = "cwoodhouse"
    }, new EntityFilter<StudyFirstRead> {
        PropertyName = "FirstRead",
        OpType = ExpressionType.LessThan,
        Value = "2011-02-01 00:00"
    }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文