全部博文(136)
分类: Oracle
2008-05-13 23:26:30
SQL> set serveroutput on SQL> declare 2 type type_parent is varray(2) of varchar2(20); 3 type record_student is record( 4 id number(10), 5 name varchar2(20), 6 parent type_parent 7 ); 8 rec_student record_student; 9 begin 10 rec_student.id := 1000000001; 11 rec_student.name := 'yuechaotian'; 12 rec_student.parent := type_parent(); 13 rec_student.parent.extend(2); 14 rec_student.parent(1) := 'my father'; 15 rec_student.parent(2) := 'my mother'; 16 17 dbms_output.put_line( 'id: ' || rec_student.id ); 18 dbms_output.put_line( 'name: ' || rec_student.name ); 19 for n_pointer in rec_student.parent.first..rec_student.parent.last loop 20 dbms_output.put_line( 'parent of ' || n_pointer || ' is: ' || rec_student.parent(n_pointer) ); 21 end loop; 22 end; 23 / id: 1000000001 name: yuechaotian parent of 1 is: my father parent of 2 is: my mother PL/SQL 过程已成功完成。 SQL> |
SQL> desc my_family 名称 是否为空? 类型 ----------------------------------------- -------- ----------- PROVINCE VARCHAR2(20) PARENT TYPE_PARENT CHILDREN TYPE_CHILDREN SQL> declare 2 rec_family my_family%rowtype; 3 begin 4 select * 5 into rec_family 6 from my_family 7 where rownum = 1; 8 9 dbms_output.put_line( 'PROVINCE: ' || rec_family.PROVINCE ); 10 11 for n_pointer in rec_family.PARENT.first..rec_family.PARENT.last loop 12 dbms_output.put_line( 'PARENT of ' || n_pointer || ' is: ' || rec_family.PARENT(n_pointer) ); 13 end loop; 14 15 for n_pointer in rec_family.CHILDREN.first..rec_family.CHILDREN.last loop 16 dbms_output.put_line( 'CHILDREN of ' || n_pointer || ' is: ' || rec_family.CHILDREN(n_pointer) ); 17 end loop; 18 end; 19 / PROVINCE: PARENT of 1 is: my father PARENT of 2 is: my mother CHILDREN of 1 is: my sister CHILDREN of 2 is: my brother CHILDREN of 3 is: yuechaotian PL/SQL 过程已成功完成。 SQL> |
parameter_name [ IN | IN OUT | OUT ] parameter_type [ DEFAULT | := |
SQL> set serveroutput on SQL> declare 2 t_myParent my_family.parent%type; 3 4 function fun_getParent( prm_province in my_family.province%type ) return type_parent 5 is 6 t_parent my_family.parent%type; 7 begin 8 select parent 9 into t_parent 10 from my_family 11 where province = prm_province; 12 return t_parent; 13 exception 14 when no_data_found then 15 return null; 16 end; 17 begin 18 t_myParent := fun_getParent( ' 19 for n_pointer in t_myParent.first..t_myParent.last loop 20 dbms_output.put_line( 'my parent ' || n_pointer || ': ' || t_myParent(n_pointer) ); 21 end loop; 22 end; 23 / my parent 1: my father my parent 2: my mother PL/SQL 过程已成功完成。 SQL> |
SQL> declare 2 myParent_1 varchar2(20); 3 myParent_2 varchar2(20); 4 5 function fun_getParent( prm_province in my_family.province%type ) return type_parent 6 is 7 t_parent my_family.parent%type; 8 begin 9 select parent 10 into t_parent 11 from my_family 12 where province = prm_province; 13 return t_parent; 14 exception 15 when no_data_found then 16 return null; 17 end; 18 begin 19 myParent_1 := fun_getParent( ' 20 myParent_2 := fun_getParent( ' 21 dbms_output.put_line( 'my parent 1: ' || myParent_1 ); 22 dbms_output.put_line( 'my parent 2: ' || myParent_2 ); 23 end; 24 / my parent 1: my father my parent 2: my mother PL/SQL 过程已成功完成。 SQL> |
学号 (NUMBER) 姓名 (VARCHAR2) 家庭成员 (type_family) 10010 yuechaotian father: mother: sister: brother: 10020 father:
CoColi
mother:
SQL> create type type_family is varray(20) of varchar2(20); 2 / 类型已创建。 SQL> create table myStudents(id number(5), name varchar2(20), family type_family); 表已创建。 SQL> insert into myStudents values(10010, 'yuechaotian', 2 type_family('father: my father', 'mother: my mother', 'sister: my sister', 'brother: my brother')); 已创建 1 行。 SQL> insert into myStudents values(10020, 'CoColi', 2 type_family('father: her father', 'mother: her mother')); 已创建 1 行。 SQL> commit; 提交完成。 SQL> set linesize 1000 SQL> select * from myStudents; ID NAME ---------- -------------------- FAMILY ------------------------------------------------------------------------------------------------- 10010 yuechaotian TYPE_FAMILY('father: my father', 'mother: my mother', 'sister: my sister', 'brother: my brother') 10020 CoColi TYPE_FAMILY('father: her father', 'mother: her mother') SQL> |
SQL> create type type_family_nested is table of varchar2(20); 2 / 类型已创建。 SQL> create table myStudents_nested(id number(5), name varchar2(20), family type_family_nested) 2 nested table family store as family_store; 表已创建。 SQL> insert into myStudents_nested values(10010, 'yuechaotian', 2 type_family_nested('father: my father', 'mother: my mother', 'sister: my sister', 'brother: my brother')); 已创建 1 行。 SQL> insert into myStudents_nested values(10020, 'CoColi', 2 type_family_nested('father: her father', 'mother: her mother')); 已创建 1 行。 SQL> commit; 提交完成。 SQL> select * from myStudents_nested; ID NAME ---------- -------------------- FAMILY --------------------------------------------------------------------------------------------------------- 10020 CoColi TYPE_FAMILY_NESTED('father: her father', 'mother: her mother') 10010 yuechaotian TYPE_FAMILY_NESTED('father: my father', 'mother: my mother', 'sister: my sister', 'brother: my brother') SQL> |
SQL> create type type_colors is table of varchar2(20); 2 / 类型已创建。 SQL> create type type_automobile is object( 2 model varchar2(30), 3 colors type_colors 4 ); 5 / 类型已创建。 SQL> |
SQL> create table automobile of type_automobile 2 nested table colors store as colors_store; 表已创建。 SQL> insert into automobile values( type_automobile('benz', type_colors('white', 'black')) ); 已创建 1 行。 SQL> commit; 提交完成。 SQL> select * from automobile; MODEL ------------------------------ COLORS --------------------------------------------------------------------------------------------- benz TYPE_COLORS('white', 'black') SQL> |