Oracle基本操作八:PL/SQL特殊数据类型%type %rowtype VARRAY TABLE RECORD
来源:互联网 发布:盈建科软件 编辑:程序博客网 时间:2024/06/05 05:50
--%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;
0 0
- Oracle基本操作八:PL/SQL特殊数据类型%type %rowtype VARRAY TABLE RECORD
- ORACLE中record、varray、table和%type、%rowtype的使用详解
- oracle中的特殊类型:%type、record、%rowtype
- %type %rowtype record table
- oracle中数据类型rowtype,Table,Object,Record
- Oracle type/rowtype/record
- 学习笔记:关于PL/SQL 集合类型record、pl/sql表、varray、nested table
- PL/SQL复合数据类型—record,集合(索引表,嵌套表,VARRAY)—之四
- Oracle PL/SQL中如何使用%TYPE和%ROWTYPE
- Oracle PL/SQL中如何使用%TYPE和%ROWTYPE
- Oracle PL/SQL中如何使用%TYPE和%ROWTYPE
- Oracle PL/SQL中如何使用%TYPE和%ROWTYPE
- Oracle PL/SQL中如何使用%TYPE和%ROWTYPE
- Oracle PL/SQL中如何使用%TYPE和%ROWTYPE
- Oracle PL/SQL中如何使用%TYPE和%ROWTYPE
- Oracle PL/SQL中如何使用%TYPE和%ROWTYPE
- Oracle PL/SQL中如何使用%TYPE和%ROWTYPE
- Oracle PL/SQL中如何使用%TYPE和%ROWTYPE
- Python turtle 库 画图 彩色螺旋线五角星太阳花
- 2118 sdut
- HDU 5880 AC自动机
- 部署ecshop之一
- hadoop-2.6.0下hive-1.2.2安装与配置
- Oracle基本操作八:PL/SQL特殊数据类型%type %rowtype VARRAY TABLE RECORD
- [计算机视觉][神经网络与深度学习]Faster R-CNN配置及其训练教程2
- 会场安排问题
- Android ActionBar应用实战,高仿微信主界面的设计
- Hihocoder 1514
- 实验吧——WriteUp&&涨姿势(2)
- [YTu]_2439(C++习题 复数类--重载运算符+)
- 20170513_请判断二叉树是不是平衡二叉树?
- 数据结构01--概要