12. Define and use variables - composite types

Composite types—similar to data in Java—

introduce

variables that hold multiple values. Mainly include these types:
1) pl/sql record
2) pl/sql table
3) nested table
4) varray

1. Composite type---pl/sql record is similar to the structure in
high-level language , it should be noted that , when referring to pl/sql record members, you must add record variables as a prefix (record variables. record members) as follows; SQL> declare   2 --define a pl/sql record type emp_record_type   3 type emp_record_type is record(   4 name emp. ename%type,   5 salary emp.sal%type,   6 title emp.job%type);   7 -- define a sun_record variable, the type of this variable is emp_record_type   8 sun_record emp_record_type;   9 begin  10 select ename,sal,job into sun_record from emp where empno=&no;  11 --output
















 12 dbms_output.put_line('Employee Name:'||sun_record.name||' Salary:'||sun_record.salary||' Position:'||sun_record.title);
 13 end;
 14 /
Employee Name:MARTIN Salary: 1250 Post: SALESMAN
PL/SQL procedure successfully completed

Example: sun_record is a record variable, and name is a record member of sun_record

 

Composite type---pl/sql table
is equivalent to array in high-level language, but it should be noted that the subscript of array in high-level language cannot be negative, while pl/sql can be negative, and the subscript of table element is not limit. Examples are as follows:

SQL> --pl/sql table instance
SQL> declare
  2 --define a pl/sql table type sun_table_type, which is an array for storing emp.ename%type data
  3 --index by binary-integer Indicates that the subscript is an integer
  4 type sun_table_type is table of emp.ename%type index by binary_integer;
  5 --define a sun_table variable, the type of this variable is sun_table_type
  6 sun_table sun_table_type;
  7 begin
  8 select ename into sun_table(0) from emp where empno=&no;
  9 dbms_output.put_line('employee name:'||sun_table(0));
 10 end;
 11 /
employee name: MARTIN
PL/SQL procedure successfully completed

Description:
sun_table_type is the type of the pl/sql table
emp.ename%type specifies the type and length of the elements of the table
sun_table is the pl/sql table variable
sun_table(0) means that the element with the subscript 0 is


what happens if the where condition is removed?
This is to put the names of all employees in the emp table into sun_table(0), and the program will report an error.


How to solve the problem of returning multiple rows of results?
The use of reference variables can solve

the following two types of variables that do not use
compound variables --- nested table (nested table)
compound variable --- variable length variable (varray)


Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327084361&siteId=291194637