PostgreSQL相当于SQL Server TVP

问题描述 投票:1回答:2

SQL Server具有表值参数,允许您将值数组作为参数传递。

实现类似PostgreSQL查询的适当方法是什么,所以我可以这样做:

select * from product where id in ($1)

我正在使用Npgsql .NET库。

https://www.nuget.org/packages/Npgsql/3.0.5

.net postgresql npgsql
2个回答
2
投票

在PostgreSQL中,您可以使用数组而不是ID列表,例如:

... where id = any('{1, 2, 3}'::int[])

要么

... where id = any(array[1, 2, 3])

这意味着id是阵列中的一个项目。

Read more about arrays operators and functions.

要将数组作为参数从第三方语言传递,您至少可以使用第一个变体:

... where id = any($1 ::int[])

其中$ 1是一个字符串参数,看起来像{1, 2, 3}。请注意$1::int[]之间的空格 - 对于某些客户来说可能是必要的。

不确定C#是否直接支持数组参数。


1
投票

在Postgres中,您可以通过两种方式使用IN运算符:

expression IN (value [, ...])
expression IN (subquery)

阅读文档:qazxsw poi,qazxsw poi或qazxsw poi。

© www.soinside.com 2019 - 2024. All rights reserved.