0

我正在做一个学校作业,我需要得到“代码”的最后一个值,这样我就可以插入下一行,这个代码递增。我试着用这种方式把它拉出来。

DECLARE

   v_last_code f_shifts.code%TYPE;

BEGIN

   SELECT LAST_VALUE(code) OVER (ORDER BY code)
   INTO v_last_code
   FROM f_shifts;

   DBMS_OUTPUT.PUT_LINE('Last value is: ' || v_last_code);

END;

但是我明白ORA-01422: exact fetch returns more than one requested number of rows 了,我不知道为什么以及如何让 last_value 超过一行

谢谢 !

4

1 回答 1

0

您可以使用这样的嵌套表。

DECLARE
   v_last_code f_shifts.code%TYPE;
   TYPE t_tbl IS TABLE OF f_shifts.code%TYPE;
      -- Above line creates the nested table type of the required type.
   v_tbl T_TBL;
      -- Above line creates the nested table variable.
BEGIN

   SELECT code
   BULK COLLECT INTO v_tbl -- collects all the values, ordered, into the nested table
   FROM f_shifts
   ORDER BY code;

   v_last_code = v_tbl(v_tbl.LAST); -- gets the last values and puts into the variable

   DBMS_OUTPUT.PUT_LINE('Last value is: ' || v_last_code);

END;
/
于 2013-03-19T11:01:30.537 回答