Create Record based on table column type : RECORD « PL SQL Data Types « Oracle PL/SQL Tutorial






SQL> set serveroutput on
SQL> set echo on
SQL> -- create demo table
SQL> create table Employee(
  2    ID                 VARCHAR2(4 BYTE)         NOT NULL,
  3    First_Name         VARCHAR2(10 BYTE),
  4    Salary             Number(8,2)
  5  )
  6  /

Table created.

SQL>
SQL> -- prepare data
SQL> insert into Employee(ID,  First_Name, Salary)
  2               values ('01','Jason', 1234.56)
  3  /

1 row created.

SQL> insert into Employee(ID,  First_Name, Salary)
  2                values('02','Alison', 6661.78)
  3  /

1 row created.

SQL>
SQL>
SQL> -- display data in the table
SQL> select * from Employee
  2  /


ID   FIRST_NAME     SALARY
---- ---------- ----------
01   Jason         1234.56
02   Alison        6661.78

SQL>
SQL>
SQL> SET ECHO ON
SQL>
SQL> DECLARE
  2      TYPE EmployeeTypee IS RECORD (
  3          id employee.id%type,
  4          name employee.first_name%type,
  5          salary employee.salary%type
  6      );
  7
  8      emp EmployeeTypee;
  9  BEGIN
 10      SELECT * INTO emp
 11      FROM employee
 12      WHERE id = 2;
 13
 14      DBMS_OUTPUT.PUT_LINE('emp_record.id: ' || emp.id);
 15      DBMS_OUTPUT.PUT_LINE('emp_record.id: ' || emp.name);
 16      DBMS_OUTPUT.PUT_LINE('emp_record.id: ' || emp.salary);
 17  END;
 18  /
emp_record.id: 02
emp_record.id: Alison
emp_record.id: 6661.78

PL/SQL procedure successfully completed.

SQL>
SQL>
SQL> -- clean the table
SQL> drop table Employee
  2  /

Table dropped.

SQL>
SQL>
SQL>








21.30.RECORD
21.30.1.Records
21.30.2.Accessing Individual Record elements
21.30.3.Accessing an entire record
21.30.4.Testing for equality of records
21.30.5.Record Variables Based on Tables
21.30.6.Record Variables
21.30.7.Create Record based on table column type
21.30.8.Implicit Declaration
21.30.9.Assigning Record Variables
21.30.10.Using the Record Datatype and its limitation
21.30.11.Records based on tables can also be used in a SELECT statement
21.30.12.Inserts and updates using record variables
21.30.13.Using Records with 'select into'
21.30.14.Retrieving Cursor Variables with a Record Variable
21.30.15.Looping through Records in a Cursor
21.30.16.Looping through multiple records
21.30.17.Perform a field-by-field comparison
21.30.18.Passing Variables without Copying