我必须创建一个while循环,其中1 + 1 = 2..1 + 4 = 5 ..所有的方式也是4 + 4 = 8,我得到了第一部分,但我坚持下一步做什么
DECLARE
v_recno testloop1.rec_no%TYPE :=1;
v_ctr testloop1.ctr%TYPE :=1;
v_datain testloop1.data_in%TYPE :=2;
BEGIN
WHILE v_recno = 1 AND v_ctr < 5 LOOP
INSERT INTO testloop1(rec_no, ctr, data_in)
VALUES(v_recno, v_ctr, v_datain);
v_recno :=v_recno;
v_ctr :=v_ctr + 1;
v_datain :=v_recno + v_ctr;
END LOOP;
END;
/
SQL> @ loop
PL/SQL procedure successfully completed.
SQL> select *
2 from testloop1;
REC_NO CTR DATA_IN
---------- ---------- ----------
1 1 2
1 2 3
1 3 4
1 4 5
答案 0 :(得分:0)
如果我理解你想要实现的目标,你只需将你的循环包裹在外部循环中。因此,一个循环将从1.4开始迭代v_recno(外循环),另一个循环将遍历v_ctr(内循环)。这样的事情(未经测试):
DECLARE
v_recno testloop1.rec_no%TYPE :=1;
v_ctr testloop1.ctr%TYPE :=1;
v_datain testloop1.data_in%TYPE :=1; --change from 2 to 1
BEGIN
WHILE v_recno < 5 LOOP
WHILE v_ctr < 5 LOOP
v_datain :=v_recno + v_ctr; --Moved up before insert statement
INSERT INTO testloop1(rec_no, ctr, data_in)
VALUES(v_recno, v_ctr, v_datain);
v_ctr :=v_ctr + 1;
END LOOP;
v_recno := v_recno + 1;
v_ctr := 1; --Added this to reset
END LOOP;
END;
/
使用基本循环:
DECLARE
v_recno testloop1.rec_no%TYPE :=1;
v_ctr testloop1.ctr%TYPE :=1;
v_datain testloop1.data_in%TYPE :=1;
BEGIN
LOOP
LOOP
v_datain :=v_recno + v_ctr;
INSERT INTO testloop1(rec_no, ctr, data_in)
VALUES(v_recno, v_ctr, v_datain);
v_ctr :=v_ctr + 1;
IF v_ctr > 4 THEN
exit;
END IF;
END LOOP;
v_recno := v_recno + 1;
v_ctr := 1;
IF v_recno > 4 THEN
exit;
END IF;
END LOOP;
END;
/