Initialize Record in Declaration Section!
DECLARE
TYPE species_rt IS RECORD
(
species_name VARCHAR2 (100),
habitat_type VARCHAR2 (100),
surviving_population INTEGER
);
l_elephant species_rt := species_rt ('Elephant', 'Savannah', '10000');
PROCEDURE display_species (
species_in species_rt DEFAULT species_rt ('Not Set', 'Global', 0))
IS
BEGIN
DBMS_OUTPUT.put_line ('Species: ' || species_in.species_name);
DBMS_OUTPUT.put_line ('Habitat: ' || species_in.habitat_type);
DBMS_OUTPUT.put_line ('# Left: ' || species_in.surviving_population);
END;
BEGIN
display_species (l_elephant);
/* Use the default */
display_species ();
END;
Species: Elephant
Habitat: Savannah
# Left: 10000
Species: Not Set
Habitat: Global
# Left: 0
create table species
(
species_name VARCHAR2 (100),
habitat_type VARCHAR2 (100),
surviving_population INTEGER
)
Table created.
Sorry, Not For %ROWTYPE Record Types!
DECLARE
l_elephant species%rowtype := species%rowtype ('Elephant', 'Savannah', '10000');
BEGIN
NULL;
END;
ORA-06550: line 2, column 36: PLS-00431: bulk SQL attributes must use a single indexMore Details: https://docs.oracle.com/error-help/db/ora-06550
Use Named Notation in Qualified Expression
DECLARE
TYPE species_rt IS RECORD
(
species_name VARCHAR2 (100),
habitat_type VARCHAR2 (100),
surviving_population INTEGER
);
l_elephant species_rt
:= species_rt (species_name => 'Elephant',
surviving_population => '10000',
habitat_type => 'Savannah');
PROCEDURE display_species (
species_in species_rt DEFAULT species_rt ('Not Set', 'Global', 0))
IS
BEGIN
DBMS_OUTPUT.put_line ('Species: ' || species_in.species_name);
DBMS_OUTPUT.put_line ('Habitat: ' || species_in.habitat_type);
DBMS_OUTPUT.put_line ('# Left: ' || species_in.surviving_population);
END;
BEGIN
display_species (l_elephant);
/* Use the default */
display_species ();
END;
Species: Elephant
Habitat: Savannah
# Left: 10000
Species: Not Set
Habitat: Global
# Left: 0
Types Must Match!
DECLARE
TYPE species1_rt IS RECORD
(
species_name VARCHAR2 (100),
habitat_type VARCHAR2 (100),
surviving_population INTEGER
);
TYPE species2_rt IS RECORD
(
species_name VARCHAR2 (100),
habitat_type VARCHAR2 (100),
surviving_population INTEGER
);
l_species1 species1_rt := species1_rt ('Elephant', 'Savannah', '10000');
l_species2 species2_rt;
BEGIN
l_species2 := l_species1;
END;
ORA-06550: line 20, column 18: PLS-00382: expression is of wrong typeMore Details: https://docs.oracle.com/error-help/db/ora-06550
Field by Field for Different "Same" Types
DECLARE
TYPE species1_rt IS RECORD
(
species_name VARCHAR2 (100),
habitat_type VARCHAR2 (100),
surviving_population INTEGER
);
TYPE species2_rt IS RECORD
(
species_name VARCHAR2 (100),
habitat_type VARCHAR2 (100),
surviving_population INTEGER
);
l_species1 species1_rt := species1_rt ('Elephant', 'Savannah', '10000');
l_species2 species2_rt;
BEGIN
l_species2 :=
species2_rt (l_species1.species_name,
l_species1.habitat_type,
l_species1.surviving_population);
END;
Statement processed.