从单个函数更新一行中的多个记录
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果“来自选择的很多数字”不取决于正在更新的每条记录,您可以使用:
否则,如果“来自选择的很多数字”取决于每条记录正在更新,您可以使用类似以下内容:
If "a lot of numbers which come from a select" does not depend on each record being updated, you may use:
Otherwise, if "a lot of numbers which come from a select" depends on each record being updated, you may use something like:
一种方法是从 plpgsql 函数中调用更新,并从返回 3 个输出值的更新函数中调用重力函数。例如,Out1、out2 和 out3。比在您的更新中使用这些值。
伪代码:
参见 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:
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.
虽然我无法真正看到 SQL_CALCULATE_GRAVITY 函数如何返回 3 个单独的数字,但如果您确定它确实如此,那么您可以使用三个变量来读取这 3 个数字:
然后您可以使用这些变量来更新另一个表:
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:
Then you can use those variables to update another table:
您应该能够像这样直接访问字段:
如果 SQL_CALCULATE_GRAVITY() 是稳定/不可变的,则每行只会调用一次。
You should be able to access the fields directly like so:
If SQL_CALCULATE_GRAVITY() is stable/immutable, it'll only be called once per row.