要求删除id为100-200 300-400 500-600...的数据
我写了一个有问题,麻烦大家帮我改下
begin
for i in 1 .. 50
loop
delete from test_append2 where id between i*100 and (i+1)*100;
i:=i+1;
end loop;
commit;
end;

解决方案 »

  1.   

    commit;放在end loop;前啊 删100条就提交一次。
      

  2.   

    SQL> begin
      2   for i in 1 .. 50
      3   loop
      4   delete from test_append2 where id between i*100 and (i+1)*100;
      5   i:=i+1;
      6   end loop;
      7   commit;
      8   end;
      9  /
     i:=i+1;
     *
    第 5 行出现错误:
    ORA-06550: 第 5 行, 第 2 列:
    PLS-00363: 表达式 'I' 不能用作赋值目标
    ORA-06550: 第 5 行, 第 2 列:
    PL/SQL: Statement ignored
      

  3.   

    你的不是在存储过程中写的话   就得在BEGIN 之前加上 DECLARE  i  integer;来定义变量  而且循环变量不能重新赋值的。
      

  4.   

    你的i都没有定义 后面 i:=i+1;这样肯定是错误的啊   
    要declare i number;
      

  5.   

    不需要自加_dexter@DAVID> begin
      2  for i in 1 .. 50 loop 
      3  dbms_output.put_line(i) ;
      4  end loop; 
      5  end ;
      6  /每次loop结束会隐式的i = i + 1   1  begin
      2  for i in 1 .. 50 loop
      3  dbms_output.put_line(i) ;
      4  i := i+1 ;
      5  end loop;
      6* end ;
    _dexter@DAVID> /
    i := i+1 ;
    *
    ERROR at line 4:
    ORA-06550: line 4, column 1:
    PLS-00363: expression 'I' cannot be used as an assignment target
    ORA-06550: line 4, column 1:
    PL/SQL: Statement ignored
      

  6.   

    在匿名块
    _dexter@DAVID> begin
      2  for i in 1 .. 50 loop 
      3  dbms_output.put_line(i) ;
      4  end loop; 
      5  end ;
      6  /
    中,i的叫做loop index ,当loop 循环体内尝试改变 loop index 的值,就会报PLS-00363 这个错误。
      

  7.   

    你好,如果我要实现题目要求,根据id的取值对表进行间隔删除数据,该怎么实现呢?
    比如:删除id为100-200 300-400 500-600...的数据,保留201-299 401-499 ....的数据
      

  8.   

    既然每次loop结束会会隐式的i = i + 1  那就在where条件的i上做处理
    id为100-200 300-400 500-600...的数据 可以看出每次都是从奇到偶 那么就会得到between 2*(i-1)*100 and (2*i-1)*100
      

  9.   

    between 2*(i-1)*100 and 2*i*100
      

  10.   

    between (2*i-1)*100 and 2*i*100 晕