--%type %rowtype
%type 前面必须有前缀
定义某个变量的数据与已存在的变量数据类型、某个列的数据类型相同。
好处:1.可能不知道数据表中字段的数据类型
2.数据库中字段的数据类型可能运行时已被改变,程序变量的类型会随着改变
declare
v_id tb_student.id%type; --v_id的类型与表tb_student中id的类型一样
v_name tb_student.name%type;
v_age tb_student.agee%type;
begin
select id,name,age
into v_id,v_name,v_age
from tb_student
where id=1;
dbms_output.putline('V_id:'||v_id);--打印输出id
end;
%rowtype
用于定义不确定的类型的变量,可以理解为对数据库记录一行提取处理的一个副本
declare
r_tb_student tb_student%rowtype;--r_tb_student表示表tb_student的一行数据(包括数据类型)
begin
select id,name,age
into r_tb_student.id,r_tb_student.name,r_tb_student.age
from tb_student
where id=1;
dbms_output.putline('r_tb_student.id:'||r_tb_student.id);--打印输出id
--或者
select *
into r_tb_student
from tb_student
where id=1;
dbms_output.putline('r_tb_student.id:'||r_tb_student.id);--打印输出id
end;
--复合数据类型(可变数组VARRAY/TABLE/RECORD)
--VARRAY
declare
--定义一个数组,对象名为strings,5个元素
type strings is varray(5) of varchar2(10);
--不能直接使用,使用前需要用一个变量去引用,如v_list
v_list strings:=strings('1','2','3','4','5');--赋值
begin
dbms_output.putline(v_list(1));--打印第一个元素
end;
--TABLE 索引表,能够模仿数组的永久表,类似于数组
注:1.index-by表中的元素不一定要按任何特定的顺序排列
2.关键字(索引)唯一允许的类型是binary_integer
3.使用关键字(索引)引用table中的field,索引可正可负
declare
--定义对象,类型是table
type strings is table of varchar(10)
--下标无界限binary_integer
index by binary_integer;
--不能直接引用,用v_list引用
v_list strings;
begin
v_list(1):='Hello';--赋值
v_list(9999):='world';
dbms_output.putline(v_list(1)||v_list(9999));
--结果:Helloworld
end
--RECORD类型:包含一个或几个组件(Field/域),域的类型可以是变量或任意数据类型,类似于集合
declare
type v_record is record(
id number,
name tb_student.name%type,
r_tb_student tb_student%rowtype
);
v_student v_record;--不能直接用,赋给变量
begin
select id,name,age
into v_student.id,
v_student.name,
v_student.r_tb_student.age
from tb_student
where id=1;
dbms_output.putline('r_tb_student.id:'||r_tb_student.id);--打印输出id
end;
%type 前面必须有前缀
定义某个变量的数据与已存在的变量数据类型、某个列的数据类型相同。
好处:1.可能不知道数据表中字段的数据类型
2.数据库中字段的数据类型可能运行时已被改变,程序变量的类型会随着改变
declare
v_id tb_student.id%type; --v_id的类型与表tb_student中id的类型一样
v_name tb_student.name%type;
v_age tb_student.agee%type;
begin
select id,name,age
into v_id,v_name,v_age
from tb_student
where id=1;
dbms_output.putline('V_id:'||v_id);--打印输出id
end;
%rowtype
用于定义不确定的类型的变量,可以理解为对数据库记录一行提取处理的一个副本
declare
r_tb_student tb_student%rowtype;--r_tb_student表示表tb_student的一行数据(包括数据类型)
begin
select id,name,age
into r_tb_student.id,r_tb_student.name,r_tb_student.age
from tb_student
where id=1;
dbms_output.putline('r_tb_student.id:'||r_tb_student.id);--打印输出id
--或者
select *
into r_tb_student
from tb_student
where id=1;
dbms_output.putline('r_tb_student.id:'||r_tb_student.id);--打印输出id
end;
--复合数据类型(可变数组VARRAY/TABLE/RECORD)
--VARRAY
declare
--定义一个数组,对象名为strings,5个元素
type strings is varray(5) of varchar2(10);
--不能直接使用,使用前需要用一个变量去引用,如v_list
v_list strings:=strings('1','2','3','4','5');--赋值
begin
dbms_output.putline(v_list(1));--打印第一个元素
end;
--TABLE 索引表,能够模仿数组的永久表,类似于数组
注:1.index-by表中的元素不一定要按任何特定的顺序排列
2.关键字(索引)唯一允许的类型是binary_integer
3.使用关键字(索引)引用table中的field,索引可正可负
declare
--定义对象,类型是table
type strings is table of varchar(10)
--下标无界限binary_integer
index by binary_integer;
--不能直接引用,用v_list引用
v_list strings;
begin
v_list(1):='Hello';--赋值
v_list(9999):='world';
dbms_output.putline(v_list(1)||v_list(9999));
--结果:Helloworld
end
--RECORD类型:包含一个或几个组件(Field/域),域的类型可以是变量或任意数据类型,类似于集合
declare
type v_record is record(
id number,
name tb_student.name%type,
r_tb_student tb_student%rowtype
);
v_student v_record;--不能直接用,赋给变量
begin
select id,name,age
into v_student.id,
v_student.name,
v_student.r_tb_student.age
from tb_student
where id=1;
dbms_output.putline('r_tb_student.id:'||r_tb_student.id);--打印输出id
end;