oracle array

来源:互联网 发布:kahn算法 编辑:程序博客网 时间:2024/06/05 03:28
--1, 固定长度数组set Serveroutput On DECLARE  -- 定义varray 定义size ,of varchar2(10) 定义类型  TYPE arry_var IS VARRAY(3) OF VARCHAR2(10);  arry_name arry_var;BEGIN  -- init array  arry_name := arry_var('china', 'japan','usa');  dbms_output.put_line(arry_name(1));END; --2, 可变长度set serveroutput ondeclare  type t_table  is table of Varchar2(15) index by binary_integer;  v_arr_table t_table;  cnt int;begin  v_arr_table(1) := 'china';  v_arr_table(2) := 'USA';    cnt := v_arr_table.count;  for i in 1 .. cnt loop    dbms_output.put_line(v_arr_table(i));    end loop;end;--3, 多维create table country(  id int,  name varchar2(15));insert into country values(1,'china');insert into country values(2,'USA');commit;set serveroutput ondeclaretype t_r_country is record(  id country.id%type,  name country.name%type  );  type t_array_country is table of t_r_country index by binary_integer;  --或者这里使用  type t_array_country1 is table of country%rowtype index by binary_integer;  v_array_country t_array_country;  v_array_country1 t_array_country1;begin  select id,name bulk collect into v_array_country from country;  for i in 1 .. v_array_country.count loop    dbms_output.put_line(v_array_country(i).name);    end loop;      for i in 1 .. v_array_country.count loop    dbms_output.put_line(v_array_country(i).name);    end loop;end;--4,对象类型create or replace type obj_country is object(id int,name varchar2(15));create or replace type tab_country is  table of obj_country ;set serveroutput ondeclare  v_array_country tab_country := tab_country();beginfor cur in (select id,name from country) loop    v_array_country.extend;    v_array_country(v_array_country.count) :=obj_country(cur.id,cur.name);    end loop;end;

0 0