PL/pgSQL的anyelement例子

http://www.postgresonline.com/journal/archives/239-The-wonders-of-Any-Element.html

定义函数

pgsql=# CREATE OR REPLACE FUNCTION diff_inc(IN anyelement, IN anyelement
pgsql(#   , OUT diff integer, OUT f_val anyelement, OUT l_val anyelement)
pgsql-# RETURNS record
pgsql-# AS
pgsql-# $$
pgsql$# BEGIN
pgsql$#    diff = ($1 - $2)::integer;
pgsql$#    f_val = $1 +1;
pgsql$#    l_val = $2 +1; 
pgsql$# END;
pgsql$# $$
pgsql-# language plpgsql;
CREATE FUNCTION
pgsql=# 

带入不同的数据类型进行运算:

pgsql=# select (diff_inc(12,14)).*;
 diff | f_val | l_val 
------+-------+-------
   -2 |    13 |    15
(1 row)


pgsql=# SELECT (diff_inc('2011-12-31'::date, '2012-01-05'::date)).*;
 diff |   f_val    |   l_val    
------+------------+------------
   -5 | 2012-01-01 | 2012-01-06
(1 row)

pgsql=# 

但是,它有一个限制:一旦第一个参数确定,后面的参数必须和它的类型一致。

posted @ 2013-07-15 10:04  健哥的数据花园  阅读(963)  评论(0编辑  收藏  举报