postgresql SELECT INTO 具有多个属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16311634/
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 00:53:44  来源:igfitidea点击:

SELECT INTO with more than one attribution

sqlpostgresqlvariable-assignmentplpgsql

提问by felipe.zkn

This instruction works:

该指令有效:

SELECT INTO unsolvedNodes array_agg(DISTINCT idDestination)
FROM road 
WHERE idOrigin = ANY(solvedNodes)
AND NOT (idDestination = ANY(solvedNodes));

But I would like to use something this way:

但我想这样使用一些东西:

SELECT INTO unsolvedNodes array_agg(DISTINCT idDestination), lengths array_agg(length)
FROM road
WHERE idOrigin = ANY(solvedNodes)
AND NOT (idDestination = ANY(solvedNodes));

How to use only one "SELECT INTO" instruction to set multiple variables?

如何仅使用一个“SELECT INTO”指令来设置多个变量?

回答by Erwin Brandstetter

In PL/pgSQLyou can SELECT INTOas many variables at once as you likedirectly. You just had the syntax backwards:

PL/pgSQL 中,您可以直接直接使用任意数量的SELECT INTO变量。你只是把语法倒过来了:

SELECT INTO unsolvedNodes, lengths 
       array_agg(DISTINCT idDestination), array_agg(length)
FROM   road
WHERE  idOrigin = ANY(solvedNodes)
AND    NOT (idDestination = ANY(solvedNodes));

You have the keyword INTOfollowed by a list of target variables, and you have a corresponding SELECTlist. The target of the INTOclause can be (quoting the manual here):

您有关键字INTO后跟目标变量列表,并且您有相应的SELECT列表。该INTO子句的目标可以是(此处引用手册):

...a record variable, a row variable, or a comma-separated list of simple variables and record/row fields.

...记录变量、行变量或逗号分隔的简单变量和记录/行字段列表。

Also:

还:

The INTOclause can appear almost anywhere in the SQL command. Customarily it is written either just before or just after the list of select_expressions in a SELECTcommand, or at the end of the command for other command types. It is recommended that you follow this convention in case the PL/pgSQL parser becomes stricter in future versions.

INTO子句几乎可以出现在 SQL 命令中的任何位置。通常,它写在SELECT命令中 select_expressions 列表之前或之后,或者写在其他命令类型的命令末尾。建议您遵循此约定,以防 PL/pgSQL 解析器在未来版本中变得更加严格。

This is not to be confusedwith SELECT INTOin the SQL dialect of Postgres- which nobody should be using any more. It goes against standard SQL and will eventually be removed, most likely. The manual actively discourages its continued use:

不要SELECT INTOPostgres 的 SQL 方言混淆- 没有人应该再使用它了。它违背了标准 SQL,最终很可能会被删除。该手册积极劝阻其继续使用:

It is best to use CREATE TABLE ASfor this purpose in new code.

最好CREATE TABLE AS在新代码中用于此目的。

回答by Ali Bagheri

Yes,

是的,

SELECT name,family INTO cName, cFamily FROM "CommonUsersModel";

OR

或者

SELECT INTO cName, cFamily name,family  FROM "CommonUsersModel"