首页 > 代码库 > PL/SQL 记录 Record 简介

PL/SQL 记录 Record 简介

记录类型是表中当行数据结构的一个镜像。每个记录只存储一行数据,记录包含的是字段,而不是列。
1.用%rowtype属性隐式定义记录类型
declare
    individual individuals%rowtype   --记录变量individual与individuals表的结构构成镜像,拥有表的所有字段
begin
    individual.individual_id:=1;     --对变量的各个字段初始化
    individual_id.first_name:=Jone;
    individual_id.last_name:=Steve;

    insert
    into individuals                --将字段插入到individuals表中
    vaules
    (individual.individual_id,
    individual_id.first_name,
    individual_id.last_name);

    commit;
    end;
    /

 

2.显式的将记录定义为PL/SQL记录类型
declare
    type individual_record is record  --显shi的定义记录类型 individual_record
    (individual_id integer,
     first_name varchar(30 char),
     last_name varchar(30 char)
     );
begin
     individual  individual_record;   --定义 individual_record的一个变量 individual
     individual.individual_id:=1;     --对变量的各个字段初始化
     individual_id.first_name:=Jone;
     individual_id.last_name:=Steve;
     
    insert into
    individuals                --将字段插入到individuals表中
    vaules
    (individual.individual_id,
    individual_id.first_name,
    individual_id.last_name);
commit
end;
/
3.显式的将记录类型定义为对象类型
--create a database object type
create or replace type individual_record as object  --对象类型头
(
    individual_id interger,
    first_name varchar(30 char),
    last_name varchar(30 char),

    constructor function individual_record
    (
        individual_id interger,
        first_name varchar,
        last_name varchar
    )
    return self as result
)
instantiable not final;
/

--crate a database object body                  --对象主体
create or replace type body individual_record as
    constructor function individual_record
    (
        individual_id interger,
        first_name varchar,
        last_name varchar
    )
    return self as result is                 --构造函数实现
    begin
        self.individual_id:=individual_id;
        self.first_name:=first_name;
        selg.last_name:=last_name;
        return;
    end;
end;
/


declare
    individual individual_record;        --用对象类型定义individual记录变量
begin
    individual:=
        individual(1,Jone,Steve);     --调用构造函数初始化记录变量

    insert into
    individuals                --将字段插入到individuals表中
    vaules
    (individual.individual_id,
    individual_id.first_name,
    individual_id.last_name);

    commit;
end;
/