gpt4 book ai didi

PostgreSQL:函数中的嵌套 CASE 条件表达式?

转载 作者:行者123 更新时间:2023-11-29 13:31:11 28 4
gpt4 key购买 nike

如何使用 PostgreSQL 在函数中使用嵌套的 CASE 条件表达式?

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

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);

最佳答案

case 中不能有命令。使用大小写作为参数来raise

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
-----

关于PostgreSQL:函数中的嵌套 CASE 条件表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22759579/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com