PostgreSQL:函数中的嵌套大小写条件表达式?

PostgreSQL:函数中的嵌套大小写条件表达式?,postgresql,case,plpgsql,Postgresql,Case,Plpgsql,如何在使用PostgreSQL的函数中使用嵌套大小写条件表达式 例如,创建以下函数以计算两个数字: create or replace function fun(n integer) returns void as $body$ declare a int :=10; b int :=5; addition int :=0; subs int :=0; begin select n, case when n=1 then addition:=a+b;

如何在使用PostgreSQL的函数中使用嵌套大小写条件表达式

例如,创建以下函数以计算两个数字:

create or replace function fun(n integer) returns void as
$body$
declare
   a int :=10;
   b int :=5;
   addition int :=0;
   subs int :=0;

begin
   select n,
   case when n=1 then
   addition:=a+b;
   raise info '%',addition;

   case when n=2 then
   subs:=a-b;
   raise info '%',subs;

   end
end;
$body$
language plpgsql;
--调用函数

select fun(1);

案例中不能有命令。将大小写用作参数,以
升高

create or replace function fun(n integer) returns void as
$body$
declare
    a int := 10;
    b int := 5;

begin
    raise info '%', case n
        when 1 then a + b
        else a - b
        end
    ;
end;
$body$
language plpgsql;

select fun(1);
INFO:  15
 fun 
-----

(1 row)

select fun(2);
INFO:  5
 fun 
-----