我不知道为什么我的第二个DBMS_OUTPUT.OUT_LINE没有打印出我的列表?我收到匿名阻止完成并且标题(RANK:BABY NAMES :)打印出来但是我请求的列表没有。这是我的代码:
SET SERVEROUTPUT ON
SET VERIFY OFF;
DECLARE
V_SEARCH VARCHAR2(20):= '&SV_SEARCH';
V_ROWS NUMBER(11) := '&SV_ROWS';
CURSOR C_NAME IS
SELECT RANK() OVER (ORDER BY CODE DESC)
FROM (SELECT * FROM "NAME_LIST" WHERE "NAMES" LIKE (V_SEARCH)
ORDER BY CODE DESC)WHERE ROWNUM = V_ROWS;
R_NAME C_NAME%ROWTYPE;
BEGIN
OPEN C_NAME;
DBMS_OUTPUT.PUT_LINE('RANK: BABY NAMES:');
LOOP
FETCH C_NAME INTO R_NAME;
EXIT WHEN C_NAME%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(' '||V_ROWS||' '||V_SEARCH);
END LOOP;
CLOSE C_NAME;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error');
END;
结果我收到这个作为我的输出: 匿名块完成 排名:BABY NAMES:
答案 0 :(得分:1)
我会自己运行游标查询以验证您是否正在获取数据。我的猜测是你最后的ROWNUM导致你没有数据。请尝试使用此游标查询:
CURSOR C_NAME IS
SELECT RANK() OVER (ORDER BY CODE DESC)
FROM (SELECT ROWNUM AS RN, NL.* FROM "NAME_LIST" NL WHERE "NAMES" LIKE (V_SEARCH)
ORDER BY CODE DESC)WHERE RN = V_ROWS;
答案 1 :(得分:-2)
ROWNUM = 1或者它不如某事。