带有if语句的Postgresql函数 [英] Postgresql function with if statement

查看:4136
本文介绍了带有if语句的Postgresql函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使此伪代码在Postgresql中工作:

How can I make this pseudo code to work in Postgresql:

create or replace function getf(arg character varying(255)) returns int
as $$
if arg = 'a' then return 1;
else return 2;
$$ language sql;

基于参数,我需要返回一些值,没有其他表需要查询。只需要在函数内部构建一个逻辑。
尝试用if子句替换if,但无法弄清楚该怎么做。

Based on argument I need to return some values and there is no other table I need to query. Just need to build a logic inside function. Tried to replace if with when clause, but could not figure out how to do it.

谢谢!

推荐答案

create or replace function getf(arg character varying(255)) returns int as $$
begin
  if arg = 'a' then
    return 1;
  else 
    return 2;
  end if;
end; $$ language plpgsql;

请注意,这是一个PL / pgSQL函数。

Note that this is a PL/pgSQL function.

在线手册在 PL / pgSQL 上有一章非常出色。这应该为您提供开始编写过程函数所需的一切,并对逻辑分支有足够的支持。

The online manual has an excellent chapter on PL/pgSQL. That should provide everything you need to get started writing procedural function with ample support for logical branching.

这篇关于带有if语句的Postgresql函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆