但是,在PL / SQL块中使用前导分析功能,结果无法显示。请指导。
SET SERVEROUTPUT ON
DECLARE
CURSOR C_11 IS SELECT * FROM EMP;
TYPE DD IS TABLE OF EMP%ROWTYPE;
CC DD;
TYPE RR IS RECORD
(
ID EMP.ID%TYPE,
ENAME EMP.ENAME%type,
JOB EMP.JOB%TYPE,
HIREDATE EMP.HIREDATE%TYPE,
HIREDATE_LEAD EMP.HIREDATE%TYPE
);
HH RR;
BEGIN
OPEN C_11;
LOOP
FETCH C_11 BULK COLLECT INTO CC LIMIT 1000;
FOR I IN 1..cc.count
loop
SELECT ID,ENAME,JOB,HIREDATE, LEAD(HIREDATE) OVER(ORDER BY HIREDATE) into HH FROM EMP where id=cc(i).id;
DBMS_OUTPUT.PUT_LINE (HH.ID ||' '||HH.ENAME||' '||HH.JOB ||' '||HH.HIREDATE||' ' || HH.HIREDATE_LEAD);
end loop;
EXIT WHEN C_11%NOTFOUND;
END LOOP;
CLOSE C_11;
END;
答案 0 :(得分:1)
在处理Record
和BULK
运算时,需要确保遵循正确的语法。请参见下面的工作代码和内联注释:
DECLARE
CURSOR c_11 IS SELECT * FROM emp;
TYPE dd IS TABLE OF emp%rowtype;
cc dd;
TYPE rr IS RECORD (
id emp.empno%TYPE,
ename emp.ename%type,
job emp.job%TYPE,
hiredate emp.hiredate%TYPE,
hiredate_lead emp.hiredate%TYPE
);
-- This is needed to be created to hold records.
Type v_rr is table of rr index by pls_integer;
hh v_rr;
BEGIN
OPEN c_11;
LOOP
FETCH c_11 BULK COLLECT INTO cc LIMIT 1000;
FOR i IN 1..cc.count
loop
SELECT empno,
ename,
job,
hiredate,
nvl(LEAD(hiredate) OVER( ORDER BY hiredate) ,hiredate)
Bulk Collect into hh -- Since you are using collection,you need to use BULK collect
FROM emp;
--where empno = cc(i).empno; -- This looks redundant.
-- Iterate through the loop to print the result.
For j in 1.. hh.count
loop
dbms_output.put_line(hh(j).id || ' ' || hh(j).ename || ' ' || hh(j).job || ' ' || hh(j).hiredate || ' ' || hh(j).hiredate_lead);
end loop;
end loop;
EXIT WHEN c_11%notfound;
END LOOP;
CLOSE c_11;
END;
输出:
SQL> /
7369 SMITH CLERK 17-DEC-80 20-FEB-81
7499 ALLEN SALESMAN 20-FEB-81 22-FEB-81
7521 WARD SALESMAN 22-FEB-81 02-APR-81
7566 JONES MANAGER 02-APR-81 01-MAY-81
7698 BLAKE MANAGER 01-MAY-81 09-JUN-81
7782 CLARK MANAGER 09-JUN-81 08-SEP-81
7844 TURNER SALESMAN 08-SEP-81 28-SEP-81
7654 MARTIN SALESMAN 28-SEP-81 17-NOV-81
7839 KING PRESIDENT 17-NOV-81 03-DEC-81
7900 JAMES CLERK 03-DEC-81 03-DEC-81
7902 FORD ANALYST 03-DEC-81 23-JAN-82