CREATE OR REPLACE PROCEDURE pro_emp (nu IN NUMBER) ISCURSOR cur_emp IS SELECT ename FROM (SELECT ename,row_number()over(ORDER BY sal DESC) rn FROM emp) t WHERE t.rn<=nu;BEGIN FOR i IN cur_emp LOOP dbms_output.put_line(i.ename); END LOOP;END;要求如下:sql>exec pro_emp(2); sql>have top 2 salary of person are king scott ford(备注:因为scott和ford收入都是3000,所以都为第2高收入的人)
-
CREATE OR REPLACE PROCEDURE pro_emp_top2 (nu IN NUMBER) IS
-
CURSOR cur_emp IS SELECT ename,sal FROM (SELECT ename,sal,row_number()over(ORDER BY sal DESC) rn FROM emp1) t ORDER BY rn ;
-
pname emp1.ENAME%TYPE;
-
pname2 emp1.ENAME%TYPE;
-
psal emp1.SAL%TYPE;
-
psal2 emp1.SAL%TYPE;
-
i NUMBER :=0;
-
j NUMBER :=0;
-
k NUMBER;
-
BEGIN
-
SELECT COUNT(*) INTO k FROM emp1;
-
OPEN cur_emp;
-
WHILE i<nu LOOP
-
FETCH cur_emp INTO pname,psal;
-
dbms_output.put_line(pname);
-
i:=i+1;
-
END LOOP;
-
WHILE j<k-nu LOOP
-
FETCH cur_emp INTO pname2,psal2;
-
IF psal2=psal THEN
-
dbms_output.put(pname2);
-
dbms_output.put_line(' 发生并列第'||nu||'名!');
-
ELSE
-
NULL;
-
END IF;
-
j:=j+1;
-
END LOOP;
-
CLOSE cur_emp;
-
END;
test:
-
PL/SQL procedure successfully completed
-
-
SQL> exec pro_emp_top2(2);
-
-
KING
-
SCOTT
-
FORD
-
-
PL/SQL procedure successfully completed
-
-
SQL>
注释:
对于你提的这个问题,在逻辑上,一般没考虑,但在实际应用当中,可能的确会存在。增加了第二循环,就可以判定,当最后一个有并列(按该维度排序)时,把并列的全部取出来。
阅读(1101) | 评论(0) | 转发(0) |