select 到 PostgreSQL 中的多个变量

select into multiple variables in PostgreSQL

我在一次分配多个变量时遇到问题。 运行下面的代码

select v1, v2 into x, y from (values (1,2)) as t (v1, v2);

抛出错误:

ERROR:  syntax error at or near ","
LINE 1: select v1, v2 into x, y from (values (1,2)) as t (v1, v2);

Link 到 dbfiddle:

https://dbfiddle.uk/?rdbms=postgres_11&fiddle=98285b190de7871354ccb444d17eb25f

有人能帮忙吗?

谢谢。

在每个输出旁边分别使用别名,如下所示:

select v1 as x, v2 as y from (values (1,2)) as t(v1,v2);

SQL 不支持 Postgres 中的变量。您可以在 PL/pgSQL language, in a function or an anonymous code block 中使用这种赋值,例如:

do $$
declare 
    x int; 
    y int;
begin
    select v1, v2 into x, y 
    from (values (1,2)) as t (v1, v2);
end $$;

db<>fiddle.