【问题标题】:dapper sql in-clause throws exceptiondapper sql in-clause 抛出异常
【发布时间】:2023-03-27 14:45:01
【问题描述】:

这可能与SELECT * FROM X WHERE id IN (...) with Dapper ORM 重复

我正在努力实现:

connection.execute("delete from table where id in @ids", new { ids = new int[]{1,2}});

但它不起作用。我总是得到:错误:42883:运算符不存在:整数=整数[]。

即使我这样做:

connection.Query<a>("select * from a where a_id in @ids", new { ids = new int[] { 12, 13 } })

我得到了同样的例外。 我正在使用 Npgsql 访问 postgresql 数据库。 你能告诉我我做错了什么吗?

这是第二条语句在数据库中发生的情况:

这是第二条语句的一些日志:

运算符不存在:integer = integer[] at character 33

没有运算符与给定名称和参数类型匹配。您可能需要添加显式类型转换。

select * from a where a_id in ((array[12,13])::int4[])

这是第一个(与上面相同,但最后一行不同)

从 where a_id in ((array[12,13])::int4[]) 中删除

【问题讨论】:

  • 这真的很奇怪......我看不出你的 c# 有什么问题。您是否对服务器的实际访问有 SQL 跟踪?
  • 我已经用数据库中的一些语句更新了这个问题
  • 这是我最近几天看到的第二份报告......我强烈怀疑实际上是特定的 ado.net 提供程序(postgres)在这样做,因为根本不是我们生成的 TSQL...我将不得不调查
  • 非常感谢,同时,我会将该功能置于待机状态。你能指出我在哪里可以找到调查的进展吗?
  • 相关:stackoverflow.com/questions/25297173/…。可能需要where id = ANY @ids

标签: sql postgresql dapper in-clause


【解决方案1】:

我建议您查看 Postgres 文档 Searching in Arrays。 简而言之,您应该使用运算符“ANY”或“ALL”或手动检查列中的每个数组值。

此 sql 是带有 IN 子句的查询的等效版本:

delete from table where id = any (@ids)

【讨论】:

  • 请添加一些解释,说明此代码为何/如何帮助 OP。这将有助于为未来的观众提供一个可以学习的答案。请参阅this Meta question and its answers 了解更多信息。
【解决方案2】:
var query = "SELECT * FROM lookup WHERE LOWER(discriminator) = ANY(@types)";
_connection.QueryAsync<Lookup>(query, new { types = new[] {"Prefix", "Suffix"} });

这对我有用

【讨论】:

【解决方案3】:

什么对我有用: connection.Query("select * from a where a_id = ANY(@ids)", new { ids = new int[] { 12, 13 } })

【讨论】:

    猜你喜欢
    • 2021-11-23
    • 2014-12-10
    • 2021-04-19
    • 2016-01-06
    • 2017-01-11
    • 1970-01-01
    • 2013-05-24
    • 2021-03-01
    • 1970-01-01
    相关资源
    最近更新 更多