我在空表中出入十行数据。 id 不是主键,我只是测试一下for  in  loop 
的用法,用一下语句可以插入: declare 
i number:=1; 
cursor c is select id from sun_t; 
v_c number; 
begin 
open c; 
loop 
fetch c into v_c; 
exit when(i=10); 
if(c%rowcount=0)then 
insert into sun_t(id) values (0); 
dbms_output.put_line(c%rowcount); 
end if; 
i:=i+1; 
end loop; 
close c;
commit; 
end; 但是用for确不能插入?????还是搞不等????高手赐教呀???? 
declare 
i number:=1; 
cursor c is select id from sun_t; 
begin 
for v_c in c 
loop 
exit when(i=10); 
if(c%rowcount=0)then 
insert into sun_t(id) values (0); 
dbms_output.put_line(c%rowcount); 
end if; 
i:=i+1; 
end loop; 
commit; 
end;

解决方案 »

  1.   

    用for的話  你就不用定義游標了  直接
    for aa in (select id from sun_t)  loop    end loop  就行了
      

  2.   

    改成如下:DECLARE
      I NUMBER := 1;
      cursor c is select id from sun_t; 
    BEGIN
      FOR V_C IN C LOOP
        --EXIT WHEN(I = 10);
        --IF (C%ROWCOUNT = 0) THEN
          INSERT INTO SUN_T (ID) VALUES (0);
          DBMS_OUTPUT.PUT_LINE('xx'||C%ROWCOUNT);
        --END IF;
        --I := I + 1;
      END LOOP;
      COMMIT;
    END;
    /
      

  3.   

    二楼的写法  我试了
      还是不行?????why???
    declare 
    i number:=1; 
    begin 
    for v_c in (select id from sun_t)
    loop 
    exit when(i=10); 
    if(sql%rowcount=0)then 
    insert into sun_t(id) values (0); 
    dbms_output.put_line(sql%rowcount); 
    end if; 
    i:=i+1; 
    end loop; 
    commit; 
    end;
      

  4.   


    。。  也沒有sql了o