我必须生成 PL/SQL 块,该块输入一个整数,其中 n (1 ≤ n ≤ 100) 并显示最流行的男性和女性名称。如果输入的数字超出该范围,则显示无效输入。我创建了一个名为流行名称的表,其中包含 100 个最流行的男性和女性名称。我正在为我的 FOR 循环而苦苦挣扎,并让它与用户输入一起工作。我的代码如下:
ACCEPT p_1 prompt 'please enter an integer between 1 and 100'
DECLARE
CURSOR name_cur IS
SELECT rank, male_given_name, female_given_name
FROM popular_names
ORDER BY rank;
v_nrank popular_names.rank%TYPE := &p_1;
v_nmale popular_names.male_given_name%TYPE;
v_nfemale popular_names.female_given_name%TYPE;
BEGIN
OPEN name_cur;
FETCH name_cur INTO v_nrank, v_nmale, v_nfemale;
IF name_cur%FOUND THEN
DBMS_OUTPUT.PUT_LINE('Rank Male Given Name Female Given Name');
DBMS_OUTPUT.PUT_LINE('----------------------------------------------');
FOR i IN 1..v_nrank LOOP
DBMS_OUTPUT.PUT_LINE((RPAD(v_nrank, 18) || ' ' ||
(RPAD(v_nmale, 18) || ' ' || v_nfemale)));
FETCH name_cur INTO v_nrank, v_nmale, v_nfemale;
EXIT WHEN name_cur%NOTFOUND;
END LOOP;
ELSE
DBMS_OUTPUT.PUT_LINE('Invalid number!');
END IF;
CLOSE name_cur;
END;