The employee ID is the primary key, so the lookup can only ever find one row. And yet SELECT ... INTO fails with ORA-01422: exact fetch returned more than the requested number of rows .
The cause: the parameter has the same name as the column . I reproduced it on Oracle AI Database 26ai Free (23.26.3) and ran the check and the fix. All output below is real SQL*Plus output.
Reproduce
Three employees, and a function that returns a name for an ID. The parameter is called emp_id , same as the column.
CREATE TABLE emp ( emp_id NUMBER PRIMARY KEY , ename VARCHAR2 ( 20 ) ) ; INSERT INTO emp VALUES ( 101 , 'Kim' ) ; INSERT INTO emp VALUES ( 102 , 'Lee' ) ; INSERT INTO emp VALUES ( 103 , 'Park' ) ; COMMIT ; CREATE OR REPLACE FUNCTION get_ename ( emp_id IN NUMBER ) RETURN VARCHAR2 IS v_name emp . ename % TYPE ; BEGIN SELECT ename INTO v_name FROM emp WHERE emp_id = emp_id ; RETURN v_name ; END ; /
Enter fullscreen mode
Exit fullscreen mode
It compiles fine ( Function created. ).…