OraclePL/SQL高级编程(一)

2014-11-24 16:41:48 · 作者: · 浏览: 0

1、 复合数据类型――记录类型

语法格式

type 类型名 is record (

字段1 字段1类型 [not null]:=表达式1;

字段2 字段2类型 [not null] :=表达式2;

… )

说明:记录之间相互赋值要求两个记录类型完全相同。

案例

举例1

--创建表并插入记录

create table student(idnumber, name varchar2(20), birthday date);

insert into studentvalues(100, 'xiaoming', to_date('2000.12.17', 'yyyy.mm.dd'));

insert into studentvalues(200, 'xiaohua', to_date('2001.12.17', 'yyyy.mm.dd'));

insert into studentvalues(300, 'xiaoli', to_date('2002.12.17', 'yyyy.mm.dd'));

--定义记录类型

Declare

type t_studentRecord isrecord(

id number,

name varchar2(20),

birthday date);

或者:

type t_StudentRecord isrecord(

idstudent.id%type,

name student.name%type,

birthday student birthday%type);

v_students t_StudentRecord;

begin

select * into v_students from students whereid=200;

dbms_output.put_line(v_students.id||' '||v_students.name||' '||to_char(v_students.birthday,'yyyy-mm-dd'));

end;

举例2

declare

type t_StudentRecord is record(

id number(4),

name varchar2(20),

birthday date);

v_students t_StudentRecord;

v_students_copy t_StudentRecord;

begin

v_students.id:=400;

v_students.name:='赵伟';

v_students.birthday:= to_date('2003.12.17','yyyy.mm.dd');

v_students_copy:=v_students;

dbms_output.put_line(v_students_copy.id||''||v_students_copy.name||' '|| to_char(v_students_copy. birthday, 'yyyy-mm-dd'));

end;

举例3

declare

type t_StudentRecord is record(

id number(4),

name varchar2(20),

birthday date);

type t_StudentRecord2 is record(

id number(4),

namevarchar2(20),

birthday date);

v_students t_studentRecord;

v_students_copy t_studentRecord2;

begin

v_students.id:=400;

v_students.name:='赵伟';

v_students.birthday:= to_date('2003.12.17','yyyy.mm.dd');;

v_students_copy:=v_students;

dbms_output.put_line(v_students_copy.id||''||v_students_copy.name||' '|| to_char(v_students_copy. birthday, 'yyyy-mm-dd'));

end;

出错说明:如果两个记录类型类型名不同,但是内容完全相同,两个类型对应的两个变量不能互相赋值。

2、 集合数据类型――index-by表

介绍:类似于普通程序设计语言中的数组概念。

声明index-by表的方法:

Type 类型名 IS TABLE OF typeINDEX BY BINARY_INTEGER;

说明:其中type定义的是index-by表中各元素的类型,可以是内置类型、用户定义的对象类型或者使用%rowtype的表达式等。

index-by表中单个元素

在声明了类型和变量后,可以通过:变量名(index)使用表中的单个元素,其中index是指表中的第几个元素。

Index by表的属性函数

属性名称

数据类型

说 明

count

number

返回表中的行数

delete

用于从表中删除指定(由传入参数指定)的一行数据

exists

boolean

如果指定的行存在则返回true,否则返回false

first

binary_integer

返回表中第一行的下标

last

binary_integer

返回表中最后一行的下标

next

binary_integer

返回指定行(由传入参数指定)的下一行的下标

prior

binary_integer

在指定行(由传入参数指定)的上一行的下标

案例

举例1

declare

type t_StudentRecord isrecord(

idstudent.id%type,

name student.name%type,

birthdaystudent birthday%type);

type t_studentTable is table oft_StudentRecord index by binary_integer;

v_students t_studentTable;

begin:

select * into v_students(100) from student whereid=100;

dbms_output.put_line(v_students(100).id||''||v_students(100).name||' '|| to_char(v_students(100). birthday, 'yyyy-mm-dd'));

end;

举例2

declare

type t_studentTable is table of student%rowtypeindex by binary_integer;

v_students t_studentTable;

begin

select * into v_students(1) from student whereid=200;

dbms_output.put_line(v_students(1).id ||''||v_students(1).name||'

'|| to_char(v_students(1). birthday,'yyyy-mm-dd'));

end;

举例3

Declare

Type t_s is table of scott.emp%rowtype Indexby binary