当前位置:Gxlcms > 数据库问题 > 【小计】PostgreSQL实现三元表达式功能

【小计】PostgreSQL实现三元表达式功能

时间:2021-07-01 10:21:17 帮助过:233人阅读


create or replace function decode(p_condition boolean, p_fist_val text, P_last_val text)
returns text
 as
$$
declare
 v_ret_val text;
begin
 /*
 * 功能说明:模拟三元表达式  ( condition ? value1 : value2 );
 * 参数说明:p_condition 接收 boolean类型的表达式 如: 1 = 1,  2 > 1 等; 后两个值是根据p_condition的真假对应的返回值
 * 实现原理: p_condition 为真则返回p_fist_val, 否则返回P_last_val
 */
 v_ret_val := null;
 if true = p_condition then
  v_ret_val := p_fist_val;
 elsif false = p_condition then
  v_ret_val := P_last_val;
 end if;
 return v_ret_val;
end;
$$
 language plpgsql;


-- 测试数据
select decode(1=2, ‘outer‘, decode(1=1, ‘inter1‘, ‘inter2‘));


本文出自 “积跬步至千里” 博客,请务必保留此出处http://chnjone.blog.51cto.com/4311366/1658846

【小计】PostgreSQL实现三元表达式功能

标签:postgresql   三元表达式   

人气教程排行