23.PLSQL中的记录类型

        PLSQL中的记录类型Record类似于C语言中的结构体,无非就是将若干相关联的字段组合成一个整体,假设有如下的表格,使用自定义的Record类型将其读取,它的使用方法归纳如下:


一、使用普通的数据类型定义Record

declare
 TYPE mydept_type IS RECORD(
   dnum number,
   dname varchar2(20)
 );
 v_dept  mydept_type;
begin
   select deptnum,dname into v_dept from mydept where deptnum=1;
   dbms_output.put_line(v_dept.dnum||'、'||v_dept.dname);
end;
/

二、使用“字段%type”的形式定义Record

declare
  type mydept_type is record(
  dnum mydept.deptnum%type,
  dname mydept.dname%type
  );
  v_dept mydept_type;
begin 
  select * into v_dept from mydept where deptnum=1;
  dbms_output.put_line(v_dept.dnum||'、'||v_dept.dname);
end;
/

三、利用Record类型向数据库中插入数据

--利用Record类型向数据库中插入数据
declare
  type mydept_type is record(
  dnum mydept.deptnum%type,
  dname mydept.dname%type
  );
  v_dept mydept_type;
begin
  v_dept.dnum := 2;    --赋值语句一定要在begin块中
  v_dept.dname := 'TEST';
  INSERT INTO mydept VALUES V_DEPT; --values后面直接跟着定义的record类型的变量
end;
/

四、利用Record类型向数据库中修改数据

declare
  type mydept_type is record(
  dnum mydept.deptnum%type,
  dname mydept.dname%type
  );
  v_dept mydept_type;
begin
  v_dept.dnum := 2;
  v_dept.dname := '软件中试部';
  update mydept set row = v_dept where deptnum=2; --使用关键字row
end;
/

五、Record和“表名%rowtype”

        之前使用过下面的方法获取过mydept的数据
declare
  v_dept mydept%rowtype;
begin
  select * into v_dept from mydept where deptnum=1;
  dbms_output.put_line(v_dept.deptnum||'、'||v_dept.dname);
end;
/

        rowtype其实就是一种固定了字段的Record,而Record的灵活之处就在于可以由我们对字段进行自定义,比如利用一行中某几个字段组成一个Record。


















评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值