Hello I’m a bit baffled that the cursor I have in this function is only returning the top row.
I’ve been comparing it to a few different examples I’ve seen and I just don’t see what’s wrong. Any guidance is greatly appreciated.
FUNCTION FS_A_FUNCTION
(
inDate DATE
)
RETURN VARCHAR2 IS
tAnswer VARCHAR2(1) := 'N';
tDates DATE;
CURSOR c1 IS
SELECT S.Dates FROM A_TABLE S;
BEGIN
OPEN c1;
LOOP
FETCH c1 INTO tDates;
EXIT WHEN c1%NOTFOUND;
END LOOP;
CLOSE c1;
IF inDate IN (tDates) THEN
tAnswer := 'Y';
END IF;
RETURN (tAnswer);
END FS_A_FUNCTION
Thanks in advance.
Not quite sure what you’re expecting to happen. After your loop
tDateswill have a single value from whichever row the cursor saw last. As yourselectdoesn’t have anorder by, that could be any value from your table. I think you may mean to be checking the value againstinDateinside the loop.I’m not sure why you’re using a cursor at all, unless the real logic is more complicated. If you’re doing what I think, you could just do something like:
… or maybe a bit clearer bu the same logic:
In both cases I’m using
max()in case there are multiple rows with the same date.