我要实现的是WHILE 表不为空 LOOP,环境是PL/SQL中的一个存储过程这里我写的是while (select count(*) from nodecal)>0 loop但是PL/SQL总是报错,PLS-00103: 出现符号“SELECT”在需要下列之一时不知道是否我的语法有问题,那么想实现这样的功能该怎么写呢?谢谢了。

解决方案 »

  1.   

    create or replace p_test
    n number;
    begin
    select count(*) into n from nodecal;
    while n>0 loop
    ...............
    end loop;
      

  2.   

    上面有误
    create or replace procedure p_test is
    n number;
    begin
      select count(*) into n from nodecal;
      while n>0 loop
         null;--你的语句
      end loop;
    end;
      

  3.   


    declare
    v_num number;
    begin
    select count(*) into v_num from nodecal;
    while v_num>0 loop
    .....
    end loop;
    end;
      

  4.   

    建议楼主看一看书,下面这本PL/SQL书不错的,并且网上有电子书
    精通Oracle 10g PL/SQL编程
      

  5.   

    楼主以前应该是用SQL SERVER的,SQL SERVER可以这样写的,可以将SQL语句放入IF 判定或者WHILE循环条件的判定中,ORACLE对于这种是不允许的。你可能每次循环的时候都要去获取一次COUNT觉得很烦,但是这个ORACLE的规矩,其实只是写法上不一样,你那种写法即使成立也是每次需要去提取一次,所以在ORACLE里面一般来说你如果直接使用WHILE循环就需要在循环外面写一次COUNT,循环里面还要写错一次COUNT,估计你的count应该是在不断变化中的(注意程序不要走入死循环了,这个根据实际情况去判定)方法1:在循环外面获取一次COUNT,循环里面结束后还要获取一次,以得到最新的COUNT用以下一次WHILE判定。declare
      n number;
    loop
    BEGIN
      select count(*) into n from nodecal;
      while n>0 loop
        ...............执行代码片段
        select count(*) into n from nodecal;
      end loop;
    END;
    方法2:只在循环内部控制,强制跳出即可:declare
      n number;
    BEGIN
      loop
        select count(*) into n from nodecal;
        EXIT WHEN n<=0;--每次获取到的N判定如果小于等于0,则跳出循环
        .....代码片段部分
      end loop;
    END;