DB2 - create user defined function to find out factorial

Bandi
Posted by Bandi under Others category on | Points: 40 | Views : 1091
To find out the factorial of a given number, the below is the useful code to write UDFs in DB2.....

CREATE FUNCTION FACT (NUM INT)
RETURNS INTEGER
SPECIFIC FACTORIAL
BEGIN
DECLARE V_FACT INT DEFAULT 1;
IF NUM = 0 THEN
RETURN 1;
ELSEIF NUM >=1 THEN
WHILE (NUM >0) DO
SET V_FACT = V_FACT * NUM;
SET NUM = NUM - 1;
END WHILE;
RETURN V_FACT;
ELSE
RETURN -1;
END IF;
END@

-- Test the function
SELECT FACT (10) FROM SYSIBM.SYSDUMMY1

Comments or Responses

Login to post response