ORACLE PL/SQL from entry to proficient in notes - records and collections of record types (5)

How to download this book and related resources: pay attention to the WeChat public account, click the function introduction-book link to download, you can get it

illustrate

This article mainly introduces the following knowledge points:

  • Record Types
    5.1. Introduction to Record Types
    5.2. Defining Record Types
    5.3. Assigning Record Types
    5.4. Manipulating Record Types
    5.5. Using Nested Records

5.1 Introduction to record types

If the record variable is treated as a unit, it can be declared a type, which is the record type

code

DECLARE
  v_empno hyz_emp.empno%TYPE;
  v_ename hyz_emp.ename%TYPE;
BEGIN
  SELECT s.empno, s.ename
    INTO v_empno, v_ename
    FROM hyz_emp s
   WHERE s.empno = &empno;
  dbms_output.put_line(v_empno || '工号对应的名字是:' || v_ename);
END;
--如果换成一个单元
DECLARE
  TYPE emp_type IS RECORD(--此单元emp_type几乎可以当成一张表去使用,而且此方法能使代码更浅显易懂
    v_empno NUMBER,
    v_ename VARCHAR(20));
  v_emp emp_type;
BEGIN
  SELECT s.empno, s.ename
    INTO v_emp.v_empno, v_emp.v_ename
    FROM hyz_emp s
   WHERE s.empno = &empno;
  dbms_output.put_line(v_emp.v_empno || '工号对应的名字是:' ||  v_emp.v_ename);
END;

5.2 Defining record types

grammar

TYPE type_name IS RECORD
(
field_declaration
[, field_declaration]
...
) ; 

Syntax Description

type_name is used to specify the name of the record' field_declaration is used to define one or more
subtypes and its definition syntax is as follows.
field_name field_type [[NOT NULL ] {:= | DEFAULT } expression]

  • feld_name is used to specify the name of the record member, such as empno, ename and other names that conform to the Oracle naming convention
    ,
  • field_type is any data type except REF CURSOR, or
    a database column type specified with %TYPE or %ROWTYPE.

5.3 Record Type Assignment

simple assignment

record_name. field_name := expression;

Assignment Description

  • record_name is the record name, field_name is the record member field
  • expression can be any constant, variable, record, collection type, expression, function call, etc.

code example

DECLARE
  TYPE emp_type IS RECORD( --此单元emp_type几乎可以当成一张表去使用,而且此方法能使代码更浅显易懂
    v_empno NUMBER := 10, --初始化一个值
    v_ename VARCHAR(20) := '二次猿'); --初始化一个值
  v_emp emp_type;
BEGIN
  v_emp.v_empno := 20;
  v_emp.v_ename := '二次猿2';
  dbms_output.put_line(v_emp.v_empno || '工号对应的名字是:' || v_emp.v_ename);
END;

record type assignment

Type must match

code example

DECLARE
  TYPE emp_type IS RECORD( --此单元emp_type几乎可以当成一张表去使用,而且此方法能使代码更浅显易懂
    v_empno NUMBER := 10, --初始化一个值
    v_ename VARCHAR(20) := '二次猿'); --初始化一个值
  v_emp  emp_type;
  v_emp2 emp_type;

BEGIN
  v_emp.v_empno := 20;
  v_emp.v_ename := '二次猿2';
  dbms_output.put_line(v_emp.v_empno || '工号对应的名字是:' || v_emp.v_ename);
  v_emp := v_emp2;--记录类型v_emp赋值给v_emp2
  dbms_output.put_line(v_emp2.v_empno || '工号对应的名字是:' || v_emp2.v_ename);
END;

Assignment using select or fetch statement

DECLARE
  TYPE emp_type IS RECORD(--此单元emp_type几乎可以当成一张表去使用,而且此方法能使代码更浅显易懂
    v_empno NUMBER,
    v_ename VARCHAR(20));
  v_emp emp_type;
BEGIN
  SELECT s.empno, s.ename
    INTO v_emp
    FROM hyz_emp s
   WHERE s.empno = &empno;
  dbms_output.put_line(v_emp.v_empno || '工号对应的名字是:' ||  v_emp.v_ename);
END;

5.4 Manipulating record types

Using record types in INSERT statements

Using record types in UPDATE statements

Using record types in RETURNING statements

Through the above 3 points, it can be found that record variables are only allowed to be used in the following situations.

  • Record variables can be used on the right side of the SET clause in an UPDATE statement.
  • After the VALUES clause in the INSERT statement, you can use records to insert data] No parentheses are required after VALUES.
  • After the INT0 clause in the RETURNING statement, the affected row can be inserted into the record variable.
  • Record variables are not allowed in the SELECT list, WHERE clause, GROUP BY clause, or ORDER
    BY clause.

5.5 Using Nested Statements

DECLARE
  TYPE emp_job IS RECORD(
    job VARCHAR2(9));
  TYPE emp_type IS RECORD(
    v_empno NUMBER,
    v_ename VARCHAR(20),
    v_job   emp_job); --定义嵌套语句
  v_job2 emp_job;
  v_emp  emp_type;
BEGIN
  SELECT s.job INTO v_job2 FROM hyz_emp s WHERE s.empno = 5;
  v_emp.v_job := v_job2;
  SELECT s.empno, s.ename, s.job
    INTO v_emp.v_empno, v_emp.v_ename, v_job2.job
    FROM hyz_emp s
   WHERE s.empno = 5;
  dbms_output.put_line(v_emp.v_empno || '工号对应的名字是:' || v_emp.v_ename ||
                       ',对应的工作:' || v_job2.job);
END;

I am a programmer. If you think it's well organized, please pay attention to your personal WeChat public account (scan it):

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326010304&siteId=291194637