Some applications may require the use of COUNT but need to support values larger than the largest integer.
This can be achieved by use of sourced user-defined functions and setting the SQL path.
The following series of statements shows how to create a sourced function to support COUNT(*) based on COUNT_BIG and returning a decimal value with a precision
of 15.
The SQL path is set such that the sourced function based on COUNT_BIG is used in subsequent statements such as the query shown.
CREATE FUNCTION RICK.COUNT() RETURNS DECIMAL(15,0)
SOURCE SYSIBM.COUNT_BIG();
SET CURRENT PATH RICK, SYSTEM PATH;
SELECT COUNT(*) FROM EMPLOYEE;
Note how the sourced function is defined with no parameters to support COUNT(*).
This only works if you name the function COUNT and do not qualify the function with the schema name when it is used.
To get the same effect as COUNT(*) with a name other than COUNT, invoke the function with no parameters.
Thus, if RICK.COUNT had been defined as RICK.MYCOUNT instead, the query would have to be written as follows:
SELECT MYCOUNT() FROM EMPLOYEE;
If the count is taken on a specific column, the sourced function must specify the type of the column.
The following statements created a sourced function that will take any CHAR column as a argument and use COUNT_BIG to perform the counting.
CREATE FUNCTION RICK.COUNT(CHAR()) RETURNS DOUBLE
SOURCE SYSIBM.COUNT_BIG(CHAR());
SELECT COUNT(DISTINCT WORKDEPT) FROM EMPLOYEE;