Oracle PL/SQL - %ROWTYPE Attribute


The %ROWTYPE attribute defines a record variable to represent either a full or partial row of a table or view.

The record has a field with the same name and data type.

If the structure of the row changes, then the structure of the record changes accordingly.

The record fields do not inherit the constraints or initial values of the corresponding columns.

To declare a record variable that always represents a full row of a database table or view, use this syntax:

variable_name table_or_view_name%ROWTYPE; 

The following code declares a record variable that represents a row of the table departments, assigns values to its fields, and prints them.


SQL> DECLARE-- from   www .j av a2s . co  m
  2    dept_rec departments%ROWTYPE;
  3  BEGIN
  6    dept_rec.department_id   := 10;
  7    dept_rec.department_name := 'Administration';
  8    dept_rec.manager_id      := 200;
  9    dept_rec.location_id     := 1700;
 11    -- Print fields:
 13    DBMS_OUTPUT.PUT_LINE('dept_id:   ' || dept_rec.department_id);
 14    DBMS_OUTPUT.PUT_LINE('dept_name: ' || dept_rec.department_name);
 15    DBMS_OUTPUT.PUT_LINE('mgr_id:    ' || dept_rec.manager_id);
 16    DBMS_OUTPUT.PUT_LINE('loc_id:    ' || dept_rec.location_id);
 17  END;
 18  /


Related Topics