Oracle PL/SQL (3) - 记录类型(TYPE 类型

2020-03-30  本文已影响0人  乘风破浪的姐姐

对于Oracle数据类型,主要使用的是VARCHAR2、NUMBER、DATE等类型,但是这些基本数据类型,如果在进行一些实际操作的时候就会比较麻烦。
获取一个雇员的完整信息:

例如下面这个例子,各个数据类型均被单独定义.

declare
      v_claimno         emb.claim.claimno%TYPE;
      v_accidentno      emb.claim.acc_no%TYPE;
      v_totalamount     emb.claim.estimate_amount%TYPE;
      v_assigndate      emb.claim.assigndate%TYPE;
begin
      v_claimno:='541';
      select t.acc_no,t.estimate_amount,t.assigndate into v_accidentno, v_totalamount, v_assigndate from claim t where t.claimno=v_claimno;
      DBMS_OUTPUT.put_line('事故号:'||v_accidentno||'   定损总金额:'||NVL(v_totalamount,2)||'  任务分配时间:'||TO_CHAR(v_assigndate,'yyyy-mm-dd'));
  EXCEPTION
     WHEN others THEN
        RAISE_APPLICATION_ERROR(-20007,'此定损单不存在!');
end;
/

输出结果:
事故号:9040622410008F03AD
定损总金额:1862.46
任务分配时间:2020-03-30

1、定义记录类型语法

TYPE 类型名称 IS RECORD (
成员名称        数据类型 [[NOT NULL] [:= 默认值] 表达式] ,
...
成员名称        数据类型 [[NOT NULL] [:= 默认值] 表达式]
) ;
1、使用记录类型接收查询返回结果
declare
   v_claimno              emb.claim.claimno%TYPE;
   TYPE claim_type IS RECORD(
      v_accidentno      emb.claim.acc_no%TYPE,
      v_totalamount     emb.claim.estimate_amount%TYPE,
      v_assigndate      emb.claim.assigndate%TYPE         
   );
   v_claim claim_type;
begin
    v_claimno:='541';
      select t.acc_no,t.estimate_amount,t.assigndate into v_claim from claim t where t.claimno=v_claimno;
      DBMS_OUTPUT.put_line('事故号:'||v_claim.v_accidentno||'   定损总金额:'||NVL(v_claim.v_totalamount,2)||'  任务分配时间:'||TO_CHAR(v_claim.v_assigndate,'yyyy-mm-dd'));
  EXCEPTION
     WHEN others THEN
        RAISE_APPLICATION_ERROR(-20007,'此定损单不存在!');
end;
/

输出结果:
事故号:9040622410008F03AD
定损总金额:1862.46
任务分配时间:2020-03-30

2、自定义记录类型数据,声明变量,并为属性赋值.
declare
   TYPE claim_type IS RECORD(
      v_accidentno      emb.claim.acc_no%TYPE :='90406224',-- 定义默认值
      v_version     emb.claim.version%TYPE,
      v_validflag      emb.claim.valid%TYPE 
   );
   v_claim claim_type;
begin
      v_claim.v_version:='E01';-- 为记录类型成员赋值
      v_claim.v_validflag:='1';-- 为记录类型成员赋值

      DBMS_OUTPUT.put_line('事故号:'||v_claim.v_accidentno||'   版本号:'||v_claim.v_version||'  有效标识:'||v_claim.v_validflag);
  EXCEPTION
     WHEN others THEN
        RAISE_APPLICATION_ERROR(-20007,'此定损单不存在!');
end;
/

输出结果:事故号:90406224 版本号:E01 有效标识:1

3、定义嵌套的记录类型
declare
   v_claimno              emb.claim.claimno%TYPE;
   TYPE claim_type IS RECORD(
      v_accidentno      emb.claim.acc_no%TYPE,
      v_totalamount     emb.claim.estimate_amount%TYPE,
      v_assigndate      emb.claim.assigndate%TYPE         
   );
   
   TYPE config_type is RECORD (
     v_paintflag             emb.config.paintconfig%TYPE:=0,
     v_repairflag            emb.config.repairconfig%TYPE :=1,
     v_discountflag      emb.config.discountflag%TYPE:=2,
     v_claim  claim_type
   );
   
   
    v_config_type  config_type ;
begin
    v_claimno:='541';
      select t.acc_no,t.estimate_amount,t.assigndate into v_config_type.v_claim from claim t where t.claimno=v_claimno;
      DBMS_OUTPUT.put_line('事故号:'||v_config_type.v_claim.v_accidentno||'   定损总金额:'||NVL(v_config_type.v_claim.v_totalamount,2)
      ||'  任务分配时间:'||TO_CHAR(v_config_type.v_claim.v_assigndate,'yyyy-mm-dd')||'  喷漆标识'||v_config_type.v_paintflag||'  维修标识'
      ||v_config_type.v_repairflag||'  折扣标识'||v_config_type.v_discountflag);
  EXCEPTION
     WHEN others THEN
        RAISE_APPLICATION_ERROR(-20007,'此定损单不存在!');
end;
/

输出结果:
事故号:9040622410008F03AD 定损总金额:1862.46 任务分配时间:2020-03-30 喷漆标识0 维修标识1 折扣标识2

4、插入记录,利用记录类型保存数据
DECLARE
    TYPE config_type is RECORD (
     v_paintflag             emb.config.paintconfig%TYPE:=0,
     v_repairflag            emb.config.repairconfig%TYPE :=1,
     v_discountflag      emb.config.discountflag%TYPE:=2,
     v_claim  claim_type
   );
    v_config_type  config_type ;
BEGIN
  v_config_type.v_paintflag := 0 ;
  v_config_type.repairconfig := 1 ;
  v_config_type.discountflag := 2 ;
INSERT INTO config VALUES v_config_type ;     -- 直接插入记录类型的数据
END ;
/
5、不需要插入全表数据,插入指定栏位的数据
DECLARE
    TYPE config_type is RECORD (
     v_paintflag             emb.config.paintconfig%TYPE,
     v_repairflag            emb.config.repairconfig%TYPE,
     v_discountflag          emb.config.discountflag%TYPE,
     v_claim  claim_type
   );
    v_config_type  config_type ;
BEGIN
  v_config_type.v_paintflag := 0 ;
  v_config_type.repairconfig := 1 ;
  v_config_type.discountflag := 2 ;
INSERT INTO config(paintconfig,repairconfig,discountflag) VALUES (v_config_type.v_paintflag,v_config_type.v_repairflag,v_config_type.v_discountflag) ;     -- 直接插入记录类型的数据
END ;
/
6、修改数据,利用记录类型保存数据
DECLARE
    v_claimno              emb.claim.claimno%TYPE:='541';
    TYPE config_type is RECORD (
     v_paintflag             emb.config.paintconfig%TYPE,
     v_repairflag            emb.config.repairconfig%TYPE,
     v_discountflag      emb.config.discountflag%TYPE,
     v_claim  claim_type
   );
    v_config_type  config_type ;
BEGIN
  v_config_type.v_paintflag := 1 ;
  v_config_type.repairconfig := 0 ;
  v_config_type.discountflag := 1 ;
UPDATE  config SET ROW=v_config_type WHERE  claimno=v_claimno;      
END ;
/
上一篇 下一篇

猜你喜欢

热点阅读