When I call my function for a value that does not exist in the table:
SELECT pacjent_usun_PK_ERROR(5) from dual
(PESEL='5' does not exist in my table)
The function returns NULL.
But when I pass a valid value:
SELECT pacjent_usun_PK_ERROR(1) from dual
(PESEL='1' exists in my table)
The function returns 1.
Here is my function:
CREATE OR REPLACE FUNCTION pacjent_usun_PK_ERROR
( PES IN NUMBER )
RETURN NUMBER
IS
ILE NUMBER;
ZMIENNA NUMBER;
BEGIN
SELECT PACJENTID INTO ZMIENNA FROM PACJENT WHERE PESEL=PES;
SELECT COUNT(PRZYJECIEID) INTO ILE FROM PRZYJECIE_NA_ODDZIAL
WHERE PACJENTID=ZMIENNA and rownum=1;
RETURN ILE;
END;
When I test my last SELECT from my function, with manual inserted PRZYJECIEID (PACJENTID='1111' does not exist in my table)
SELECT COUNT(PRZYJECIEID) FROM PRZYJECIE_NA_ODDZIAL WHERE PACJENTID='1111' and rownum=1;
Result is: 0
and testing with PACJENTID='1' (exist in my table)
Result is: 1
I am trying to understand why the function returns NULL instead of 0 when there are no rows.
I am using Oracle SQL Developer