postgres:带有文本[]参数失败的呼叫函数与数组文字失败

发布于 2025-02-04 05:54:37 字数 973 浏览 3 评论 0 原文

我有一个Postgres函数,该功能接受文本[]作为输入。例如

create function temp1(player_ids text[])
    returns void
    language plpgsql
as
$$
begin
    update players set player_xp = 0
    where id in (player_ids);
    -- the body is actually 20 lines long, updating a lot of tables
end;
$$;

,我正在尝试称呼它,但是我一直在

[42883] ERROR: operator does not exist: text = text[] Hint: No operator matches the given name and argument types. You might need to add explicit type casts. Where: PL/pgSQL function temp1(text[]) line 3 at SQL statement

尝试这些尝试,到目前为止

select temp1('{F7AWLJWYQ5BMPKGXLMDNQKQ4NY,AQPBAFKQONGLBKIMCSOD747GY4}');
select temp1('{F7AWLJWYQ5BMPKGXLMDNQKQ4NY,AQPBAFKQONGLBKIMCSOD747GY4}'::text[]);
select temp1(array['F7AWLJWYQ5BMPKGXLMDNQKQ4NY,AQPBAFKQONGLBKIMCSOD747GY4']);
select temp1(array['F7AWLJWYQ5BMPKGXLMDNQKQ4NY,AQPBAFKQONGLBKIMCSOD747GY4']::text[]);

,我必须缺少一些明显的东西...我如何用数组字面的字面来称呼此功能?

I have a Postgres function that accepts a text[] as input. For example

create function temp1(player_ids text[])
    returns void
    language plpgsql
as
$
begin
    update players set player_xp = 0
    where id in (player_ids);
    -- the body is actually 20 lines long, updating a lot of tables
end;
$;

and I'm trying to call it, but I keep getting

[42883] ERROR: operator does not exist: text = text[] Hint: No operator matches the given name and argument types. You might need to add explicit type casts. Where: PL/pgSQL function temp1(text[]) line 3 at SQL statement

I have tried these so far

select temp1('{F7AWLJWYQ5BMPKGXLMDNQKQ4NY,AQPBAFKQONGLBKIMCSOD747GY4}');
select temp1('{F7AWLJWYQ5BMPKGXLMDNQKQ4NY,AQPBAFKQONGLBKIMCSOD747GY4}'::text[]);
select temp1(array['F7AWLJWYQ5BMPKGXLMDNQKQ4NY,AQPBAFKQONGLBKIMCSOD747GY4']);
select temp1(array['F7AWLJWYQ5BMPKGXLMDNQKQ4NY,AQPBAFKQONGLBKIMCSOD747GY4']::text[]);

I have to be missing something obvious...how do I call this function with an array literal?

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

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

发布评论

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

评论(2

瀟灑尐姊 2025-02-11 05:54:37

使用 =任何而不是 in:

    ...
    update players set player_xp = 0
    where id = any(player_ids);
    ...

在操作员中作用于显式值列表。

在(value [,...])中的表达

在要将值与数组的每个元素进行比较时,请使用 any。

表达式操作员任何(阵列表达式)

请注意,两个构建体的变体在(subquery)中的表达。尽管在这种情况下有一个子查询似乎过多,但第一个被正确使用了。

Use = any instead of in:

    ...
    update players set player_xp = 0
    where id = any(player_ids);
    ...

The IN operator acts on an explicit list of values.

expression IN (value [, ...])

When you want to compare a value to each element of an array, use ANY instead.

expression operator ANY (array expression)

Note that there are variants of both constructs for subqueries expression IN (subquery) and expression operator ANY (subquery). The first one was properly used in the other answer though a subquery seems excessive in this case.

长安忆 2025-02-11 05:54:37

您可以使用非最重要的功能,此功能非常简单且相同的时间最佳性能。 Unnest使用将数组元素转换为行。示例:

create function temp1(player_ids text[])
    returns void
    language plpgsql
as
$
begin
    update players set player_xp = 0
    where id in (select pl.id from unnest(player_ids) as pl(id));
    -- the body is actually 20 lines long, updating a lot of tables
end;
$;

您可以轻松地将数组元素施加到另一种类型,以便使用Unnest。
例子:

update players set player_xp = 0
where id in (select pl.id::integer from unnest(player_ids) as pl(id));

You can use unnest function, this function is very easy and same time best performanced. Unnest using for converting array elements to rows. Example:

create function temp1(player_ids text[])
    returns void
    language plpgsql
as
$
begin
    update players set player_xp = 0
    where id in (select pl.id from unnest(player_ids) as pl(id));
    -- the body is actually 20 lines long, updating a lot of tables
end;
$;

And you can easily cast array elements to another type for using unnest.
Example:

update players set player_xp = 0
where id in (select pl.id::integer from unnest(player_ids) as pl(id));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文