前言:
任何一门程序语言都有自己的变量,程序结构,以反应客观实际需求和应用。在PL/SQL中变量分为两类,简单变量和复合变量,本文介绍如何使用复合变量。
PL/SQL变量的分类:
一、简单变量
二、复合变量
正文
二、复合变量
(1)复核变量的数据类型:
1、table : 是一种数组数据类型,用table定义的变量可以存储多个数据。相当于把数组作为数据类型,用这个数据类型初始化不同的变量。这种变量的使用方法与数组相同。
2、record:记录类型,相当于一个记录,可以存储多个数据,类似JAVA中的类。
3、%rowtype: 定义一个复合变量,变量的类型与某个表的行数据类型相同。如:v_emp emp%rowtype;
(2)复核变量数据类型命名习惯:
1、一般情况下,TABLE类型的数据类型名前加“type_table_”,表示这个数据类型是自定义的“table”。
2、一般情况下,RECORD类型的数据类型名前加“type_record_”,表示这个数据类型是自定义的“record”。
(3)复合变量声明语法:
1、syntax for table:
type type_data_type_name is table of data_type index by binary_integer;
variable_name type_table_variable_name;
//type,is table of, index by binary_integer 是关键字;
//is table :表示定义的是数组;
//data_type:表示数组的数据类型;
//index by binary_integer:表示数组下标是二进制整数;
e.g.:
type type_table_student is table of char(20) index by binary_integer;
v_student_name type_table_student;
--首先定义了一个名叫‘type_table_student’的数据类型,这个数据类型是一个数组,元素的数据类型为'char(20)',
--下标索引是二进制整数。然后定义了一个名叫‘student_name’复合变量。这个变量的数据类型为type_table_student。
2、syntax for record:
type type_data_type_name is record //type,is record 是关键字。
(id number,
name varchar2(20),
sex char(6)
);
variable_name type_data_type_name;
e.g.:
type type_record_student is record //首先定义了一个名叫‘type_record_student’的数据类型.
(id number,
name varchar2(20), //这个数据类型包括三个成员,就像JAVA类一样
sex char(6)
);
v_student type_record_student; //定义名叫‘student’的复合变量。变量的数据类型为type_record_student.
3、syntax for %rowtype:
variable_name table_name%rowtype;
e.g.:
v_emp emp%rowtype;
--定义了一个名叫“v_emp”的复合变量,变量的每个成员的数据类型与表“emp”的各字段数据类型相同。
--变量“v_emp”的成员个数及 数据类型由“emp”的各字段数及数据类型决定。
(4)复核变量的使用方法:
1、TABLE变量的使用和数组的使用方法是一样的,如:student_name(0),student_name(1),student_name(2) := 'Han';
2、RECORD变量的使用方法,与类成员调用方法一样,如:student.name,student.id, student.sex := 'female';
实例演习:
1、TABLE复合变量的使用;
declare
type type_table_student_name is table of char(20) index by binary_integer;
v_student_name type_table_student_name;
begin
v_student_name(0) := 'HanLong';
v_student_name(1) := 'LiQi';
v_student_name(2) := 'LiHong';
v_student_name(3) := 'WangYun';
dbms_output.put_line(v_student_name(0));
dbms_output.put_line(v_student_name(1));
dbms_output.put_line(v_student_name(2));
dbms_output.put_line(v_student_name(3));
end;
2、TABLE复合变量的使用;
declare
type type_table_class_stu_count is table of number index by binary_integer;
v_class_stu_count type_table_class_stu_count;
begin
v_class_stu_count(0) := 2;
v_class_stu_count(1) := 3;
v_class_stu_count(2) := 4;
v_class_stu_count(3) := v_class_stu_count(0) + v_class_stu_count(1) + v_class_stu_count(2);
dbms_output.put_line(v_class_stu_count(0));
dbms_output.put_line(v_class_stu_count(1));
dbms_output.put_line(v_class_stu_count(2));
dbms_output.put_line(v_class_stu_count(3));
end;
3、RECORD复合变量的使用;
declare
type type_record_student is record
(id number,
name varchar2(20),
sex char(6)
);
v_student type_record_student;
begin
v_student.id := 201301;
v_student.name := 'LiQi';
v_student.sex := 'male';
dbms_output.put_line(v_student.id);
dbms_output.put_line(v_student.name);
dbms_output.put_line('The student sex is '||v_student.sex);
end;
4、%rowtype复合变量的使用;
declare
v_emp emp%rowtype;
begin
select * into v_emp from emp where userid='JC-BJ-0024'; //要求SELECT必须有数据查询到,且只能有一条记录。
dbms_output.put_line('The emp id is :'|| v_emp.userid);
dbms_output.put_line('The emp name is : '|| v_emp.name);
dbms_output.put_line('The emp sal is : '|| v_emp.sal);
end;
--select 语句如果有多条数据返回,则PL/SQL会抛出too_many_rows异常。
阅读(216) | 评论(0) | 转发(0) |