SQL Function for DB2 and ORA

19 篇文章 0 订阅
7 篇文章 0 订阅


SQL Function in DB2 9

(UDF=User Defined Function)

DB2 9 and earlier, your could only has RETURN statement in your UDF logic. (This is a too limited design)
• RETURN can’t contain a SELECT statement
• RETURN can’t include a column name
So the most cases you define a function is to receive a value (as parameter) as input, and transforming that value and returning result of that transformation

For example: define TOCHAR function
CREATE FUNCTION TOCHAR(X DECIMAL(7, 2))
 RETURNS VARCHAR(20)
 LANGUAGE SQL
 RETURN VARCHAR(X, '.');


SQL Function in DB2 10

Compared with limited design in DB2 9,  SQL function in DB2 10 UDF can do much more, it looks more likely a normal FUNCTION design.
General function logic (compared with only RETURN  in DB2 9) are supported, variable declaration are supported.

BEGIN
   DECLARE VAR1 INTEGER;
   DECLARE VAR2, VAR3 CHAR(10);
 
   SET VAR1 = …;
   IF P1 = …;
   RETURN VAR2;

END@


SQL Function in Oracle

Sample, convert a BINARY_DOUBLE to string:
CREATE OR REPLACE FUNCTION <F_NAME>(P_FLOATVAL BINARY_DOUBLE)
  RETURN VARCHAR2
IS
  V_RETURN VARCHAR2(100);
BEGIN
  V_RETURN := TO_CHAR(P_FLOATVAL, 'FM09.09999999999999EEEE');
  RETURN V_RETURN;
END;
/


Overloading SQL Function

Both DB2 and Oracle can support function overloading.

But DB2 there are some limitation:
1. It does not consider any length, precision, or scale attributes of the arguments.
    Following functions overloading will FAIL, because are not overloading, but conflicting
    - FOO( DECIMAL )         vs. FOO( DECIMAL(5) )              # precision is ignored
    - FOO( DECIMAL(5) )    vs. FOO( DECIMAL(5, 2) )           # scale
    - FOO( CHAR )              vs. FOO(  CHAR(5) )                    #
    - FOO( VARCHAR(1) )   vs. FOO(  VARCHAR(5) )             #

    But CHAR(5) and VARCHAR(5) are not conflicting, but overloading, because they treated as different data types.

2. Synonymous and inherited data types are considered identical.
    - FOO(REAL)         vs. FOO(FLOAT)
    - FOO(REAL)         vs. FOO(FLOAT)
    - FOO(CHAR)        vs. FOO(GRAPHIC)
    - FOO(VARCHAR) vs. FOO(VARGRAPHIC)
    - FOO(CLOB)         vs. FOO(DBCLOB)
    - FOO(DECIMAL)   vs. FOO(NUMERIC)

3. If the function has more than 30 parameters, only the first 30 parameters to determine whether the function unique.

And for Oracle:
TO_BE_DONE


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值