Server具有表值参数,允许将值数组作为参数传递。
实现类似于PostgreSQL查询的批准方法是什么,这样我就可以执行以下操作:
select * from product where id in ($1)
我正在使用Npgsql .NET库。
发布于 2016-02-08 17:44:15
在PostgreSQL中,可以使用数组而不是ID列表,如:
... where id = any('{1, 2, 3}'::int[])或
... where id = any(array[1, 2, 3])这意味着id是数组的项之一。
阅读有关数组、运算符和函数的更多信息。
要将数组作为来自第三方语言的参数传递,可以至少使用第一个变体:
... where id = any($1 ::int[])其中$1是一个字符串参数,类似于{1, 2, 3}。请注意,$1和::int[]之间有一个空间--对某些客户端来说可能是必要的。
不确定C#是否直接支持数组参数。
发布于 2016-02-08 15:32:14
在Postgres中,您可以通过两种方式使用In运算符:
expression IN (value [, ...])
expression IN (subquery)阅读文档:第一变体,第二变体或本综述。
发布于 2020-11-06 14:22:07
下面是一个使用Dapper和Npgsql在C#中插入的示例--它将1、2、3插入一个临时表,并选择它们从有序降序返回,因此它将将32.1打印到控制台。这里的诀窍是Postgres unnest()函数,它将数组展开为一组行:
var results = await conn.QueryAsync<int>(@"
CREATE TEMPORARY TABLE DapperNpgsqlArrayParameterDemo (id int not null primary key);
INSERT INTO DapperNpgsqlArrayParameterDemo (id) SELECT unnest(@numbers);
SELECT id from DapperNpgsqlArrayParameterDemo order by id desc;",
new { numbers = new int[] { 1, 2, 3 } });
foreach(var item in results)
{
Console.WriteLine($"found {item}.");
}https://stackoverflow.com/questions/35273060
复制相似问题