带有 case 语句的简单 Postgres UDF 不起作用
simple Postgres UDF with case statement not working
当我运行以下代码时:
create or replace function roundfind(dates date) returns varchar as $$
begin
select
case
when dates between '2020-06-08' and '2020-11-14' and dates is not null then return 'Round 1'
when dates between '2020-11-15' and '2021-02-17' and dates is not null then return 'Round 2'
when dates between '2021-02-18' and '2021-04-28' and dates is not null then return 'Round 3'
when dates between '2021-04-29' and '2021-07-16' and dates is not null then return 'Round 4'
when dates between '2021-07-16' and '2021-10-03' and dates is not null then return 'Round 5'
when dates between '2021-10-04' and '2021-11-30' and dates is not null then return 'Round 6'
when dates between '2021-12-01' and '2022-02-01' and dates is not null then return 'Round 7'
when dates between '2021-02-02' and '2022-03-28' and dates is not null then return 'Round 8'
when dates >= '2022-03-29' and dates is not null then return 'Round 9'
end
end; $$
language PLPGSQL;
postgres.roundfind(date(19-5-2007)) -- function call
总会有一些错误,最近的错误是:
“语言未定义”。虽然我已经定义清楚了。
如果使用PL/pgSQL,则需要使用RETURN QUERY SELECT ...
但是你不需要PL/pgSQL来封装一个简单的查询,使用language sql
并去掉begin ... end
另外 then return
应该是 then
create or replace function roundfind(dates date) returns varchar
as $$
select
case
when dates between '2020-06-08' and '2020-11-14' and dates is not null then 'Round 1'
when dates between '2020-11-15' and '2021-02-17' and dates is not null then 'Round 2'
when dates between '2021-02-18' and '2021-04-28' and dates is not null then 'Round 3'
when dates between '2021-04-29' and '2021-07-16' and dates is not null then 'Round 4'
when dates between '2021-07-16' and '2021-10-03' and dates is not null then 'Round 5'
when dates between '2021-10-04' and '2021-11-30' and dates is not null then 'Round 6'
when dates between '2021-12-01' and '2022-02-01' and dates is not null then 'Round 7'
when dates between '2021-02-02' and '2022-03-28' and dates is not null then 'Round 8'
when dates >= '2022-03-29' and dates is not null then 'Round 9'
end;
$$
language sql;
请注意,您的 CASE 表达式 中的 and dates is not null
条件是多余的,因为 between
仅 return true
如果值不为空。
当我运行以下代码时:
create or replace function roundfind(dates date) returns varchar as $$
begin
select
case
when dates between '2020-06-08' and '2020-11-14' and dates is not null then return 'Round 1'
when dates between '2020-11-15' and '2021-02-17' and dates is not null then return 'Round 2'
when dates between '2021-02-18' and '2021-04-28' and dates is not null then return 'Round 3'
when dates between '2021-04-29' and '2021-07-16' and dates is not null then return 'Round 4'
when dates between '2021-07-16' and '2021-10-03' and dates is not null then return 'Round 5'
when dates between '2021-10-04' and '2021-11-30' and dates is not null then return 'Round 6'
when dates between '2021-12-01' and '2022-02-01' and dates is not null then return 'Round 7'
when dates between '2021-02-02' and '2022-03-28' and dates is not null then return 'Round 8'
when dates >= '2022-03-29' and dates is not null then return 'Round 9'
end
end; $$
language PLPGSQL;
postgres.roundfind(date(19-5-2007)) -- function call
总会有一些错误,最近的错误是: “语言未定义”。虽然我已经定义清楚了。
如果使用PL/pgSQL,则需要使用RETURN QUERY SELECT ...
但是你不需要PL/pgSQL来封装一个简单的查询,使用language sql
并去掉begin ... end
另外 then return
应该是 then
create or replace function roundfind(dates date) returns varchar
as $$
select
case
when dates between '2020-06-08' and '2020-11-14' and dates is not null then 'Round 1'
when dates between '2020-11-15' and '2021-02-17' and dates is not null then 'Round 2'
when dates between '2021-02-18' and '2021-04-28' and dates is not null then 'Round 3'
when dates between '2021-04-29' and '2021-07-16' and dates is not null then 'Round 4'
when dates between '2021-07-16' and '2021-10-03' and dates is not null then 'Round 5'
when dates between '2021-10-04' and '2021-11-30' and dates is not null then 'Round 6'
when dates between '2021-12-01' and '2022-02-01' and dates is not null then 'Round 7'
when dates between '2021-02-02' and '2022-03-28' and dates is not null then 'Round 8'
when dates >= '2022-03-29' and dates is not null then 'Round 9'
end;
$$
language sql;
请注意,您的 CASE 表达式 中的 and dates is not null
条件是多余的,因为 between
仅 return true
如果值不为空。