Collection and recording

Set of records belonging to PL / SQL complex type, set to allow a plurality of the same type of process variable as a whole, like an array of Java or C language, allowing the recording of a plurality of different types as a variable whole process.

Using record types to obtain employee information

declare
  --定义记录类型
  TYPE emp_info type is record(
      empname varchar2(10),
      job varchar(9),
      sal number(7,2) 
  );
  --声明记录类型的变量
  empinfo emp_info_type;
begin
  --查询数据并保存到记录类型中
  select ename,
        job,
        sal
    into empinfo
    from emp
   where empno=&empno;
   --输出记录类型变量中保存的员工信息
   dbms_output.put_line('员工信息为:员工姓名:'
                      ||empinfo.empname
                      ||'职位:'
                      ||empinfo.job
                      ||'薪资:'
                      ||empinfo.sal 
   );

 

Type allows simultaneous recording processing set single line of data a plurality of columns, PL / SQL data allows the simultaneous processing of multiple single lines.

Collections like arrays, PL / SQL collection provides three types as follows.

1, associated table

2, nested tables

3, variable length arrays

 

Use the cursor and displays employee name index table

declare
  --定义员工名称索引表
  TYPE emp_table is table of varchar2(10)
      index by binary_integer;
  emplist emp_table;                   --定义表类型的变量
  --定义游标类型
  cusor empcursor
  is 
    select ename
    from emp;
begin
  --如果游标没有打开,则打开游标
  if not empcursor%IEOPEN
  THEN
    OPEN empcursor;
  end if;
  --从游标结果中提取所有的员工名称
  fetch empcursor 
  bulk collect into emplist;
  --使用for循环显示所有的员工名称
  for i in 1..emplist.count
    loop
      dbms_output.put_line('员工名称:'||emplist(i));
    end loop;
    close empcursor;                   --关闭游标
end;
/

 

Guess you like

Origin blog.csdn.net/fei_yanzi/article/details/88895272