HELLO everyone please am new to plsql and am confused about ROWNUM function. In fact i have a plsql code that uses 2 tables EMPLOYEE and WORKS_ON and we create an associative array and then stores the first record and second one i will put both the code the results and the tables . my question is why we put rownum <= 1 to get the second record and when i use rownum <= 2 it gives an error : this the picture of the tables and the output
SET SERVEROUTPUT ON;
DECLARE
--Declare the table
TYPE EmpSSNarray
IS TABLE OF employee.ssn%TYPE
INDEX BY SIMPLE_INTEGER;
--Declare variables using the table
ManagementList EmpSSNarray;
WorkerList EmpSSNarray;
BEGIN
--Retrieve the first Supervisor
SELECT superssn
INTO ManagementList(1)
FROM employee
WHERE superssn IS NOT NULL
AND ROWNUM <= 1;
--Retrieve the second Supervisor
SELECT superssn
INTO ManagementList(2)
FROM employee
WHERE superssn IS NOT NULL
AND ROWNUM <= 1
AND superssn <> ManagementList(1);
--Retrieve the first worker
SELECT essn
INTO WorkerList(1)
FROM works_on
WHERE hours IS NOT NULL
AND ROWNUM <= 1
AND essn NOT IN (ManagementList(1), ManagementList(2));
--Retrieve the second worker
SELECT essn
INTO WorkerList(2)
FROM works_on
WHERE hours IS NOT NULL
AND ROWNUM <= 1
AND essn NOT IN (ManagementList(1), ManagementList(2),WorkerList(1));
--Output the results
dbms_output.put_line ('Managers are: ' || ManagementList(1) || ', ' || ManagementList(2));
dbms_output.put_line ('Workers are: ' || WorkerList(1) || ', ' || WorkerList(2));
END;
ROWNUMis not a function, but pseudocolumn that's arbitrarly returned to restrict to one row wheneverROWNUM<=1. There should return one row within aSELECT .. INTO ..statement, whereasROWNUM<=2might return more than one row which would cause too_many_rows exception.