PostgreSQL相当于SQL Server的TVP [英] PostgreSQL equivalent to SQL Server's TVP
问题描述
SQL Server具有表值参数,可让您将值的数组作为参数传递。
SQL Server has Table Value Parameters which allows you to pass an array of values as a parameter.
实现类似于PostgreSQL查询的适当方法是什么?所以我可以做类似的事情:
What is the appropiate way to achieve something similar to a PostgreSQL query so I can do something like:
从*($ 1)
我正在使用Npgsql .NET库。
I'm using Npgsql .NET library.
https://www.nuget.org/packages/Npgsql/3.0.5
推荐答案
在PostgreSQL中,您可以使用数组来代替ID列表,例如:
In PostgreSQL you can use arrays instead of list of IDs like:
... where id = any('{1, 2, 3}'::int[])
或
... where id = any(array[1, 2, 3])
这意味着 id
是数组的项之一。
which means that id
is one of the array's items.
要从第三方语言中将数组作为参数传递,您至少可以使用第一个变体:
To pass array as a parameter from third party languages you can use at least first variant:
... where id = any($1 ::int[])
其中$ 1是一个字符串参数,看起来像 {1、2、3}
。请注意, $ 1
和 :: int []
之间的空格-对于某些客户端可能是必需的。
where $1 is a string parameter looks like {1, 2, 3}
. Note that a space between $1
and ::int[]
- it may be necessary for some clients.
不确定C#是否直接支持数组参数。
Not sure about C# is it supports array parameters directly.
这篇关于PostgreSQL相当于SQL Server的TVP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!