从单个函数更新一行中的多个记录

发布于 2024-10-03 10:43:40 字数 403 浏览 2 评论 0原文

我是 postgresql 的新手,可以使用指针来前进。

问题: 我有一个包含多个字段的表。其中 3 个需要使用返回 3 个数字的函数的结果进行更新。我该怎么做?

示例(显然不起作用):

更新 gravitational_relations
set x, y, z = SQL_CALCULATE_GRAVITY(来自选择的大量数字);

SQL_CALCULATE_GRAVITY 返回 3 个数字。

如果我可以将这 3 个数字保存在每个自己的变量中并在
中使用它们就好了 更新....
设置....
,
一切都会好的。

如果我应该转向新的设计,请告诉我并举例说明。

任何帮助表示赞赏。

/迈克尔

I'm new in postgresql and could use a pointer to get forward.

Problem:
I've got a table with several fields. 3 of these needs to be updated with the result from a functions which returns 3 numbers. How do I do that?

Example(which obviously doesn't work):

update gravitational_relations
set x, y, z = SQL_CALCULATE_GRAVITY(a lot of number which comes from a select);

SQL_CALCULATE_GRAVITY returns 3 numbers.

If only I could save those 3 numbers in each own variable and use them in the
update ....
set ....
,
all would be well.

If I should move on to a new design, please tell me and give examples.

Any help is appreciated.

/Michael

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

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

发布评论

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

评论(4

踏月而来 2024-10-10 10:43:40

如果“来自选择的很多数字”取决于正在更新的每条记录,您可以使用:

UPDATE gravitational_relations
SET (x, y, z) = (f.output_param_name1, f.output_param_name2, f.output_param_name3)
FROM SQL_CALCULATE_GRAVITY(a lot of numbers which come from a select) AS f;

否则,如果“来自选择的很多数字”取决于每条记录正在更新,您可以使用类似以下内容:

UPDATE gravitational_relations
SET (x, y, z) = ((f).output_param_name1, (f).output_param_name2, (f).output_param_name3)
FROM (SELECT id, SQL_CALCULATE_GRAVITY(a lot of numbers which come from a select) AS f FROM gravitational_relations) AS x
WHERE gravitational_relations.id = x.id;

If "a lot of numbers which come from a select" does not depend on each record being updated, you may use:

UPDATE gravitational_relations
SET (x, y, z) = (f.output_param_name1, f.output_param_name2, f.output_param_name3)
FROM SQL_CALCULATE_GRAVITY(a lot of numbers which come from a select) AS f;

Otherwise, if "a lot of numbers which come from a select" depends on each record being updated, you may use something like:

UPDATE gravitational_relations
SET (x, y, z) = ((f).output_param_name1, (f).output_param_name2, (f).output_param_name3)
FROM (SELECT id, SQL_CALCULATE_GRAVITY(a lot of numbers which come from a select) AS f FROM gravitational_relations) AS x
WHERE gravitational_relations.id = x.id;
怕倦 2024-10-10 10:43:40

一种方法是从 plpgsql 函数中调用更新,并从返回 3 个输出值的更新函数中调用重力函数。例如,Out1、out2 和 out3。比在您的更新中使用这些值。

伪代码:

BEGIN FUNCTION
    EXECUTE SQL_CALCULATE_GRAVITY(out1,out2,out3);

    update gravitational_relations
    set x = out1,
        y = out2,
        z = out3;
END FUNCTION

参见 http://www.postgresql.org/docs/9.0 /interactive/plpgsql-declarations.html 并搜索输出以查看返回输出参数的函数示例。

One way is to call your update from within a plpgsql function and call your gravity function from within your update function returning 3 output values. Out1, out2, and out3 for example. Than in your update use those values.

Pseudo code:

BEGIN FUNCTION
    EXECUTE SQL_CALCULATE_GRAVITY(out1,out2,out3);

    update gravitational_relations
    set x = out1,
        y = out2,
        z = out3;
END FUNCTION

See http://www.postgresql.org/docs/9.0/interactive/plpgsql-declarations.html and search for output to see an example of a function that returns output parameters.

飘过的浮云 2024-10-10 10:43:40

虽然我无法真正看到 SQL_CALCULATE_GRAVITY 函数如何返回 3 个单独的数字,但如果您确定它确实如此,那么您可以使用三个变量来读取这 3 个数字:

SELECT INTO x, y, z SQL_CALCULATE_GRAVITY();

然后您可以使用这些变量来更新另一个表:

UPDATE table1 SET field1=x, field2=y, field3=z WHERE some_id=some_number;

Although I can't really see how SQL_CALCULATE_GRAVITY function could return 3 separate numbers, if you're sure that it does, then you can use three variables to read those 3 numbers:

SELECT INTO x, y, z SQL_CALCULATE_GRAVITY();

Then you can use those variables to update another table:

UPDATE table1 SET field1=x, field2=y, field3=z WHERE some_id=some_number;
記柔刀 2024-10-10 10:43:40

您应该能够像这样直接访问字段:

UPDATE foo
SET    bar1 = (SQL_CALCULATE_GRAVITY(numbers...)).bar1,
       bar2 = (SQL_CALCULATE_GRAVITY(numbers...)).bar2,
       ...

如果 SQL_CALCULATE_GRAVITY() 是稳定/不可变的,则每行只会调用一次。

You should be able to access the fields directly like so:

UPDATE foo
SET    bar1 = (SQL_CALCULATE_GRAVITY(numbers...)).bar1,
       bar2 = (SQL_CALCULATE_GRAVITY(numbers...)).bar2,
       ...

If SQL_CALCULATE_GRAVITY() is stable/immutable, it'll only be called once per row.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文