I was wondering if the where condition in a select statement has a nullable variable. Does it have to use a IF condition to write the same select statement twice like below? Is there a better way?
FUNCTION function1(
foo IN INTEGER,
) RETURN INTEGER
ret INTEGER;
BEGIN
IF foo IS NULL THEN
SELECT COUNT(*) INTO ret FROM t WHERE t.col IS NULL;
ELSE
SELECT COUNT(*) INTO ret FROM t WHERE t.col = foo;
END IF;
RETURN ret;
END function1;
You can always do
That can be simplified to
though you may need a function-based index on
NVL(t.col, 'Some Impossible Value')to make the query performant.