postgresql Pgsql。在函数中返回字段作为插入结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18335883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 01:04:42  来源:igfitidea点击:

Pgsql. Returning field as insert result in function

postgresql

提问by dio_bless

I can return id as an INSERTresult, like

结果我可以返回 id INSERT,比如

INSERT INTO table(id,field) VALUES($id,$value) RETURNING id;

But i cannot return it and assign to variable into my function

但我不能返回它并将变量分配到我的函数中

var = INSERT INTO table(id,field) VALUES($id,$value) RETURNING id;

or

或者

SELECT INTO var INSERT INTO table(id,field) VALUES($id,$value) RETURNING id;

don't work. How can i do it?

不工作。我该怎么做?

回答by Roman Pekar

Try this:

试试这个:

INSERT INTO table(id,field)
VALUES($id,$value)
RETURNING id into var;

回答by Kirk Roybal

You could also use:

您还可以使用:

var := (INSERT INTO table(id,field) VALUES($id,$value) RETURNING id);

Readability is of course a personal preference, but I like this syntax because it shows an obvious assignment.

可读性当然是个人偏好,但我喜欢这种语法,因为它显示了一个明显的分配。