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

时间:2015-06-05 17:50:35   收藏:0   阅读:1709


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

原文:http://chnjone.blog.51cto.com/4311366/1658846

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!