ORACLE数据库入门再在屋里坐会 (21)

  每个 PL/SQL 变量都具有一个指定存储格式,值的有效范围和约束条件的数据类型, PL/SQL 提 供 了 各 种 内 置 数 据 类 型 , 包 括varchar2,number,date,recode 引用类型,大数据类型以及用户自定义类型等。PL/SQL 将这些类型分成了 4 个大类,分别为:标量数据类型、LOB 数据类型、组合【复合】数据类型、引用【参照】数据类型

1、标量数据类型

标量类型是非常常用的一种类型,没有内部组件,仅包含单个值,主要包括 number,character,date/time,boolean 类型

语法:

declare

      变量 标量数据类型

...

2、LOB数据类型 3、组合数据类型

1)record:用来存储多个值的变量称之为组合或者复合变量,其中存储的多个值可以是 PL/SQL 记录,也可以是 PL/SQL 中的表

declare

      type 组合类型名称 is record --创建一个组合类型

      (

             变量1 数据类型 ,

             ...

             变量n 数据类型

      );

      age number(3,2) ;

      组合类型变量 组合类型名称 ;   --定义一个组合类型的变量

begin

      select 字段1,...,字段n into 组合类型的变量 from 表名 where ... ;

      ...

end;

不足之处:一次只能存储一条记录的值

2)table

declare

  type 组合类型名称 is table of 数据类型 [index by binary_integer];

  组合类型变量 组合类型名称;

begin

  select 字段 into 组合类型变量(下标1) from 数据表 where ...;

   ...

  select 字段 into 组合类型变量(下标2) from 数据表 where ...;

end;

注:

      下标可以任意的整数(负数,无上下限)

      index by binary_integer : 下标自动增长,并不需要每次使用extend增加一个空间

eg1:使用by binary_integer

declare

  type my_table is table of emp.ename%type index by binary_integer;

  einfo my_table;--不需要初始化

begin

  --不必须使用extend增加一个空间且下标可以任意整数

  select ename into einfo(-1) from emp where empno=7788;

  select ename into einfo(-2) from emp where empno=7900;

  select ename into einfo(-3) from emp where empno=7902;

  dbms_output.put_line('姓名 1:'||einfo(-1)||'姓名 2:'||einfo(-2) ||'姓名 3:'||einfo(-3));

end;

eg2:不使用by binary_integer

declare

  type my_table is table of emp.ename%type ;

  einfo my_table := my_table() ; --必须初始化

begin

  einfo.extend;   --必须使用extend增加一个空间且下标从1开始

  select ename into einfo(1) from emp where empno=7788;

  einfo.extend;

  select ename into einfo(2) from emp where empno=7900;

  einfo.extend;

  select ename into einfo(3) from emp where empno=7902;

  dbms_output.put_line('姓名 1:'||einfo(1)||'姓名 2:'||einfo(2) ||'姓名 3:'||einfo(3));

end;

eg3:可以使用bulk collect一次将符合条件的数据全部写入表中

declare

   type my_table is table of emp.ename%type index by binary_integer;

     einfo my_table;

begin

  select ename bulk collect into einfo from emp ;

  for i in 1 .. einfo.count         --count返回表的记录数

  loop

    dbms_output.put_line(einfo(i));

  end loop;

end;

eg4:record与table组合类型的混合应用

declare

--第一:自定义组合类型 - recod

type myrecord is record (

  mname emp.ename%type ,

       mjob emp.job%type

) ;

  --第一:自定义组合类型 - table

  type myType is table of myrecord index by binary_integer ;

  --第二:创建组合类型的变量

  einfo myType ;

begin

  --第二:给组合类型变量赋值 

  select ename,job into einfo(1) from emp where empno=7369 ;

  select ename,job into einfo(2) from emp where empno=7499 ;

  select ename,job into einfo(3) from emp where empno=7521 ;

  dbms_output.put_line('第一个姓名:' || einfo(1).mname || ' 职位' || einfo(1).mjob) ;

  dbms_output.put_line('第二个姓名:' || einfo(2).mname || ' 职位' || einfo(2).mjob) ;

  dbms_output.put_line('第三个姓名:' || einfo(3).mname || ' 职位' || einfo(3).mjob) ;

end;

4、预定义例外

1) case_not_found 预定义例外

在开发 pl/sql 块中编写 case 语句时,如果在 when 子句中没有包含必须的条件分支,就会触发 case_not_found 例外。

2) cursor_already_open 预定义例外

当重新打开已经打开的游标时,会隐含的触发 cursor_already_open例外。

3) dup_val_on_index 预定义例外

在唯一索引所对应的列上插入重复的值时,会隐含的触发例外

4) invalid_cursorn 预定义例外

当试图在不合法的游标上执行操作时,会触发该例外

5) invalid_number 预定义例外

当输入的数据有误时,会触发该例外

6) no_data_found 预定义例外

当执行 select into 没有返回行,就会触发该例外

7) too_many_rows 预定义例外

当执行 select into 语句时,如果返回超过了一行,则会触发该例外

8) zero_divide 预定义例外

当执行 2/0 语句时,则会触发该例外

9) value_error 预定义例外

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zyzwgw.html