I want to create a PL/SQL function which is passed a table name and a condition, and which returns the number of rows than the condition meets on that table.
I creat
As illustrated above perfectly by Alex just to add make the exception handling generic as NO_DATA_FOUND will not be raised when we are doing COUNT(*).
CREATE OR REPLACE FUNCTION CHECK_EXISTS(
TABLE_NAME VARCHAR2,
CONDITION VARCHAR2)
RETURN NUMBER
AS
VAL NUMBER;
SQL_CODE VARCHAR2(2000 CHAR):= 'SELECT COUNT (*) FROM '||TABLE_NAME||' WHERE '|| CONDITION;
BEGIN
EXECUTE IMMEDIATE SQL_CODE INTO val;
RETURN VAL;
EXCEPTION
WHEN OTHERS THEN --no data found may not get raised as we are doing count(*) here
RETURN 0;
END;