十二、PL/SQL记录类型

1、概述:

记录类型是把逻辑相关的数据作为一个单元存储起来,称作PL/SQL RECORD的域,其作用是存放互不相同但逻辑相关的信息

2、定义记录类型语法

|实例1|

使用%type

set serveroutput on;

declare

    --定义一个记录类型

    type stu_record_type is record(

            v_xingming student.xingming%type,--此处写,或者;  或者,;交叉着写。都行

            v_sal        student.sal%type,

            v_classid  student.classid%type

    );

    --定义一个记录类型的变量

            v_stu_record_type stu_record_type;

    begin

        select xingming,sal,classid into v_stu_record_type from student where xuehao=&no;

        dbms_output.put_line(v_stu_record_type.v_xingming||','||v_stu_record_type.v_sal||','||v_stu_record_type.v_classid);

    end;

 

|实例2|

使用%rowtype

declare

      --变量记录类型的声明

      v_emp_record student%rowtype;

begin

      -- 通过select into 语句为变量赋值

      select * into v_emp_record from student where xuehao='1';

      -- 打印变量的值

      dbms_output.put_line('学号是'||v_emp_record.xuehao||'姓名是'||v_emp_record.xingming||'工资是'||v_emp_record.sal);

end;

/

详解:

1)%type:使一个变量的数据类型与已经定义了的表中的某一列的数据类型一致

格式:V_bianliang      表名.列明%type

2)%rowtype:使多个变量的数据类型与已经定义了的表中的某一行的数据类型一致,这样比使用多个%type来定义表中各个列的变量要简洁得多,并且不容易遗漏、出错。这样会增加程序的可维护性。

posted @ 2016-12-04 18:17  爱笑的berg  阅读(435)  评论(0)    收藏  举报