编写一个脚本程序,用它接受用户的输入。如果用户输入0,则直接结束运行;如果用户输入大于0的数,则用LOOP循环计算1到该数字之间所有整数和;如果用户输入的数小于0,则用FOR循环计算-1到该数字之间的所有整数和,如果用户输入非数字值,则引发异常,并在程序中处理可能发生的所有异常错误。刚刚接触这个,谢谢高手们啦~~

解决方案 »

  1.   


    --这个很 基础吧,写个基础点的时时,没测试
    create or replace function f_test(n varchar2)
    return number
    is
    num  number;
    result NUMBER:=0;
    begin
         begin
              select to_number(n) into num from dual;
         exception when others then 
              raise_application_error(-20012,'not a number!');
         end;
         if num=0 then
            result:=0;
         elsif num>0 then
            for i in 1..num loop
                result:=result+i;
            end loop;
         else
            for i in 1..abs(num) loop
                result:=result+i;
            end loop;
            result:=result*(-1);
         end if;
         return result;
    end;
      

  2.   

    函数可以在脚本里调用啊,你想要过程?
    把1楼的函数编程过程就是了
    类似create or replace procedure test1(i_num in varchar2, o_result out number) as
      num number;
    begin
      select to_number(i_num) into num from dual;
      exception when others then
                  raise_application_error(-20012, 'not a number!');
      end; 
      if num = 0 then o_result := 0; 
      elsif num > 0 then 
            for i in 1 .. num 
            loop 
                o_result := o_result + i;
            end loop; 
      else for i in 1 .. abs(num) 
            loop 
               o_result := o_result + i;
            end loop;
            o_result := o_result * (-1);
      end if;
    end;调用SET SERVEROUTPUT ON;
    declare
      v_result number;
      i_num varchar;
    begin
      i_num = '&inputnum';
      test1(i_num,v_result);
      DBMS_OUTPUT.put_line('RESULT IS '||v_result);
    end;
      have a try
      

  3.   

    你說的是讓它自動執行嗎?
    可以寫個定時的JOB讓它主動去執行procedure
      

  4.   

    一楼给出的就是脚本语言的解答啊
    楼主首先要明确什么是PLSQL,什么是SQL
    什么是shell