Code Listing 1: Function for returning count of rows from table

CREATE OR REPLACE FUNCTION tabcount (
   tab IN VARCHAR2, whr IN VARCHAR2 := NULL)
   RETURN PLS_INTEGER
IS
   retval PLS_INTEGER;
BEGIN
   EXECUTE IMMEDIATE 
      'SELECT COUNT(*) FROM ' || tab || 
      ' WHERE ' || NVL (whr, '1=1')
   INTO retval;

   RETURN retval;
END tabcount;