type aaa is table of number;
v_ray  aaa;有如上数组,已经在里面放了数据,现在怎样查询里面的最大值和最小值呀( ⊙ o ⊙ )?~~~帮忙看看~~

解决方案 »

  1.   

    wf@ORA10> edit
    已写入 file afiedt.buf  1  declare
      2  v_ray aaa:=aaa();
      3  v_max int;
      4  v_min int;
      5  begin
      6  v_ray.extend(3);
      7  v_ray(1):=1;
      8  v_ray(2):=2;
      9  v_ray(3):=3;
     10  select max(column_value) into v_max from table(v_ray);
     11  select min(column_value) into v_min from table(v_ray);
     12  dbms_output.put_line('最大值:'||v_max);
     13  dbms_output.put_line('最小值:'||v_min);
     14* end;
    wf@ORA10> /
    最大值:3
    最小值:1
      

  2.   


    -- nested table 类型在块中声明
      1  declare
      2   type n_tab_type is table of number;
      3   n_tab n_tab_type:=n_tab_type(1,2,3);
      4   n_max number;
      5   n_min number;
      6   i number;
      7  begin
      8   i:=n_tab.first;
      9   n_max:=n_tab(i);
     10  n_min:=n_tab(i);
     11  loop
     12    i:=n_tab.next(i);
     13    exit when i is null;
     14    n_max:=(case when n_tab(i)>n_max then n_tab(i) else n_max end);
     15    n_min:=(case when n_tab(i)<n_min then n_tab(i) else n_min end);
     16   end loop;
     17   dbms_output.put_line('Max:'||n_max||' Min:'||n_min);
     18* end;
    SQL> /
    Max:3 Min:1-- 在数据库中生成 nested table 类型
    SQL> create type n_tab_type is table of number;
      2  /Type created.SQL> declare
      2   n_tab n_tab_type:=n_tab_type(1,2,3);
      3   n_max number;
      4   n_min number;
      5  begin
      6   select max(column_value) into n_max from table(n_tab);
      7   select min(column_value) into n_min from table(n_tab);
      8   dbms_output.put_line('Max:'||n_max||' Min:'||n_min);
      9  end;
     10  /
    Max:3 Min:1