一次选择中的多个作业

Pra*_*lar 3 postgresql plpgsql postgresql-9.1 postgresql-9.3

我在 PostgreSQL 中编写了一个函数,我希望使用单个 select 语句为变量分配多个值。我已经声明了一些变量,并使用选择来为函数体内的这些变量赋值。请在下面找到函数的主体:

BEGIN        
select SUM(quantity) into v_quantity, MAX(price) into v_price from trade where id=4;
END
Run Code Online (Sandbox Code Playgroud)

当我编译该函数时,出现以下错误

ERROR:  "max" is not a known variable
LINE 20:     select SUM(quantity) into v_quantity, max(price) into v_...
Run Code Online (Sandbox Code Playgroud)

可能是什么原因?PostgreSQL 不允许通过单个选择进行多次分配吗?

Pat*_*ick 5

是的,它确实。为了使它工作,你应该只有一个选择列表和一个INTO子句,如下所示:

BEGIN        
  select SUM(quantity), MAX(price) into v_quantity, v_price from trade where id=4;
END
Run Code Online (Sandbox Code Playgroud)