PLSQL复合数据类型(待续)

本文深入探讨了SQL中复合数据类型的使用,包括单行多列数据处理、多行单列与多列数据处理,以及如何在PL/SQL中利用记录、索引表、嵌套表、VARRAY等特性进行高效数据操作。

/**

  复合数据类型

  1.单行多列的数据处理,使用记录

  2.多行单列的数据处理,使用索引表、嵌套表、VARRAY

  3.多行多列的数据处理,使用记录表

*/

 

/**

  在select into 语句中使用记录变量

*/

 

declare

    type type_emp_record is record(

         ename emp.ename%type,

         sal   emp.sal%type

    );

 

    emp_record type_emp_record;

begin

    select ename,sal into emp_record from emp where empno='7788';

 

    dbms_output.put_line(emp_record.ename || ':' || emp_record.sal);

end;

 

结果:SCOTT:3000

 

/**

  在select into 语句中使用记录成员

*/

 

declare

    type type_emp_record is record(

         ename emp.ename%type,

         sal   emp.sal%type

    );

 

    emp_record type_emp_record;

begin

    select ename into emp_record.ename from emp where empno='7788';

 

    dbms_output.put_line(emp_record.ename);

end;

 

结果:SCOTT

 

/**

    使用%rowtype定义记录变量

*/

declare

    emp_record emp%rowtype;

begin

    select * into emp_record from emp where empno='7788';

 

    dbms_output.put_line(emp_record.empno || ':' || emp_record.ename || ':' || emp_record.sal);

end;

 

结果:7788:SCOTT:3000

 

/**

    在insert语句中使用记录变量

*/

declare

    type teacher_record_type is record(

         r_id   teacher.id%type,

         r_name teacher.name%type,

         r_age  teacher.age%type

    );

 

    teacher_record teacher_record_type;

begin

    teacher_record.r_id := '1';

    teacher_record.r_name := 'name';

    teacher_record.r_age := 25;

 

    insert into teacher values teacher_record;

end;

 

/**

    在update语句中使用记录变量

*/

declare

    type teacher_record_type is record(

         r_id   teacher.id%type,

         r_name teacher.name%type,

         r_age  teacher.age%type

    );

 

    teacher_record teacher_record_type;

begin

    teacher_record.r_id := '2';

    teacher_record.r_name := 'name';

    teacher_record.r_age := 30;

 

    update teacher set row=teacher_record where id='1';

end;

 

/**

    plsql集合索引表

*/

declare

    type emp_table_type is table of varchar2(40) index by binary_integer;

    emp_table emp_table_type;

begin

    select ename into emp_table(0) from emp where emp.empno='7788';

    dbms_output.put_line(emp_table(0));

end;

 

结果:SCOTT

 

/**

    索引表中下标使用varchar2

*/    

declare

    type emp_table_record is table of number index by varchar2(40);

    emp_table  emp_table_record;

begin

    emp_table('a') := 1;

    emp_table('b') := 2;

    emp_table('c') := 3;

 

    dbms_output.put_line(emp_table('a') || emp_table('b') || emp_table('c')

                         || emp_table.first || emp_table(emp_table.last));

end;

 

结果:123a3

 

/**

    嵌套表(嵌套表可以作为表列)

*/

declare

    type emp_table_record is table of varchar2(40);

    emp_table emp_table_record := emp_table_record('tom','tom'); 

begin

    select ename into emp_table(2) from emp where empno='7788';

 

    dbms_output.put_line(emp_table(1)||emp_table(2));

end;

 

结果:tomSCOTT

 

/**

    变长数组(可以作为表列)

*/

declare

    type emp_varray_record is varray(10) of varchar2(40);

    emp_varray emp_varray_record := emp_varray_record('tom','tom');

begin

    select ename into emp_varray(2) from emp where empno='7788';

 

    dbms_output.put_line(emp_varray(1)||emp_varray(2));

end;

 

结果:tomSCOTT

 

/**

    记录表

*/

declare

    type emp_record_type is record(

         ename emp.ename%type,

         sal   emp.sal%type

    );

    type emp_table_type is table of emp_record_type index by binary_integer;

 

    emp_table emp_table_type;

begin

    select ename,sal into emp_table(0) from emp where empno='7788';

    select ename,sal into emp_table(1) from emp where empno='7369';

 

    dbms_output.put_line(emp_table(0).ename||':'||emp_table(0).sal);

    dbms_output.put_line(emp_table(1).ename||':'||emp_table(1).sal);

end;

 

结果:SCOTT:3000 SMITH:800

 

 

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值