SET SERVEROUTPUT ON
DECLARE
v_ans testloop.ctr%TYPE := 0;
v_ct testloop.rec_no%TYPE := 1;
v_ans1 NUMBER := 0;
v_ctin NUMBER := 1;
BEGIN
WHILE v_ct < 5 LOOP
WHILE v_ctin < 5 LOOP
v_ans := v_ct + v_ctin;
dbms_output.put_line(v_ct || ' + ' || v_ctin || ' = ' || v_ans);
v_ctin := v_ctin + 1;
END LOOP;
v_ct := v_ct + 1;
END LOOP;
END;
/
SET SERVEROUTPUT OFF
有没有人建议为什么? 如果不清楚,我只想尝试这样的输出:
1 + 1 = 2,1 + 2 = 3,1 + 3 = 4,2 + 1 = 3等
非常感谢您的帮助。
答案 0 :(得分:2)
您需要在外部循环中重新初始化变量:
BEGIN
WHILE v_ct < 5 LOOP
v_ctin := 1;
--------^
WHILE v_ctin < 5 LOOP
v_ans := v_ct + v_ctin;
dbms_output.put_line(v_ct || ' + ' || v_ctin || ' = ' || v_ans);
v_ctin := v_ctin + 1;
END LOOP;
v_ct := v_ct + 1;
END LOOP;
END;