需要帮助从数组中插入数据作为输入参数,使用PostgreSQL函数

发布于 2025-02-06 05:06:27 字数 470 浏览 0 评论 0原文

任何一个人都可以帮助在postgresql中创建函数,其中参数为insertDatausingArray(exp_ids array,group_id integer)和内部函数以迭代数组(exp_ids) 相应地插入

数据 ,即{3275,3845,3578,3125}和group_id 1000520-所有

InsertDatausingArray({3275,3845,3578,3578,3125},1000520)

ext_ids作为数组 在这里,Staructre是这个 group_id | exp_id 数据如下:

exp_id, group_id
    3275,   1000520
    3845,   1000520
    3578,   1000520
    3125,   1000520

让我知道是否需要进一步澄清

can any one help to create function in postgresql where parameters are insertdatausingarray(exp_ids array, group_id integer) and inside function that to iterate the array (exp_ids)
do insertion accordingly

Data
exp_ids as array i.e. {3275, 3845, 3578, 3125} and group_id 1000520 - common for all

insertdatausingarray({3275, 3845, 3578, 3125}, 1000520)

Table:
Here, staructre is this
group_id | exp_id
and data looks as below:

exp_id, group_id
    3275,   1000520
    3845,   1000520
    3578,   1000520
    3125,   1000520

let me know if any further clarification required

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

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

发布评论

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

评论(1

甜心 2025-02-13 05:06:27

您不需要功能来执行此操作,尽管如果您真的愿意,可以将SQL放入功能中。您只需要解开阵列即可。在此示例中,我使用的是with子句传递数据,但是显然您可以以某种方式执行此操作,例如您的应用程序。

WITH data(exp_ids, group_id) AS (
  VALUES ('{3275, 3845, 3578, 3125}'::int[], 1000520)
)
INSERT INTO test (exp_id, group_id)
SELECT unnest(exp_ids), group_id
FROM data;

如果您确实需要一个函数,则只是包裹在功能中的查询的主要部分:

CREATE FUNCTION my_function(p_exp_ids int[], p_group_id int) RETURNS void AS $
    INSERT INTO test (exp_id, group_id)
    SELECT unnest(p_exp_ids), p_group_id
$
LANGUAGE SQL;```

You don't need a function to do this, although you could put the SQL in a function if you really wanted to. You just need to unnest the array. In this example, I'm using a WITH clause to pass in the data, but you could obviously do this some other way, like from your application.

WITH data(exp_ids, group_id) AS (
  VALUES ('{3275, 3845, 3578, 3125}'::int[], 1000520)
)
INSERT INTO test (exp_id, group_id)
SELECT unnest(exp_ids), group_id
FROM data;

If you really need a function, it's just the main part of the query wrapped in a function:

CREATE FUNCTION my_function(p_exp_ids int[], p_group_id int) RETURNS void AS $
    INSERT INTO test (exp_id, group_id)
    SELECT unnest(p_exp_ids), p_group_id
$
LANGUAGE SQL;```
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文