我正在努力使plpgsql if语句的语法正确。
根据函数的m2参数的值,我想使用不同的where子句。
但我得到了ERROR: syntax error at or near "if"
create or replace function some_func(m1 int, m2 int)
returns table(match_id int) as
$$
begin
return query
select m.match_id from match as m
if m2 < 1 then -- ERROR: syntax error at or near "if"
where m.match_id > m1;
else
where m.match_id > m1 and m.match_id < m2;
end if;
end;
$$ language 'plpgsql';文档上说我应该这样做
39.6.2.2. IF-THEN-ELSE
IF boolean-expression THEN
statements
ELSE
statements
END IF;但似乎我已经做到了这一点,所以我一定是误解了plpgsql如何工作的其他方面。
发布于 2013-06-30 04:07:29
你需要一个case语句,而不是if语句。仅供参考,因为它是一个可怕的查询,它应该看起来更像这样:
return query
select m.match_id from match as m
where case
when m2 < 1 then m.match_id > m1
else m.match_id > m1 and m.match_id < m2
end;在您的情况下,只需重写查询而不使用大小写:
return query
select m.match_id from match as m
where m.match_id > m1
and (m2 < 1 or m.match_id < m2);https://stackoverflow.com/questions/17384187
复制相似问题