|
Home >> FAQs/Tutorials >> Oracle DBA FAQ
Oracle DBA FAQ - Working with Database Objects in PL/SQL
By: FYIcenter.com
Part:
1
2
3
4
5
6
(Continued from previous part...)
How To Retrieve Values from Data Fields in RECORD Variables?
If a variable is a RECORD variable with data fields assigned values, you can retrieve
those values out of its data fields by using fields names prefixed with variable name
as "variable.field_name". Here is a
sample script showing you how to retrieve field values from RECORD variables:
CREATE OR REPLACE PROCEDURE HELLO AS
TYPE student IS RECORD (
id NUMBER(5),
first_name VARCHAR(80),
last_name VARCHAR(80)
);
best_student student;
another_student student;
class_name VARCHAR2(80);
BEGIN
class_name := 'FYI Center 2006';
best_student.first_name := 'The';
best_student.last_name := 'Best';
DBMS_OUTPUT.PUT_LINE('Best student = ' ||
best_student.first_name || ' '
|| best_student.last_name);
END;
/
Best student = The Best
How To Define a Data Field as NOT NULL?
When defining a specific RECORD type, you can define a data field as NOT NULL
to make sure variables with this RECORD type to always have values in this field.
A field defined as NOT NULL must have a default value. Here is a tutorial script
showing you how to define a data field as NOT NULL:
CREATE OR REPLACE PROCEDURE HELLO AS
TYPE student IS RECORD (
id NUMBER(5) NOT NULL DEFAULT 0,
first_name VARCHAR(80),
last_name VARCHAR(80)
);
best_student student;
another_student student;
class_name VARCHAR2(80);
BEGIN
class_name := 'FYI Center 2006';
best_student.first_name := 'The';
best_student.last_name := 'Best';
DBMS_OUTPUT.PUT_LINE('Best student ID = '
|| best_student.id);
DBMS_OUTPUT.PUT_LINE('Best student = ' ||
best_student.first_name || ' '
|| best_student.last_name);
END;
/
Best student ID = 0
Best student = The Best
How To Define a RECORD Variable to Store a Table Row?
If you have a table, and want to define a RECORD variable to store all the data elements
of a row from that table, you can use table_name%ROWTYPE to define the RECORD variable as
shown in the following sample script:
CREATE TABLE student (id NUMBER(5) PRIMARY KEY,
first_name VARCHAR(80) NOT NULL,
last_name VARCHAR(80) NOT NULL);
Table created.
CREATE OR REPLACE PROCEDURE FYI_CENTER AS
best_student student%ROWTYPE;
another_student student%ROWTYPE;
class_name VARCHAR2(80);
BEGIN
class_name := 'FYI Center 2006';
best_student.first_name := 'The';
best_student.last_name := 'Best';
DBMS_OUTPUT.PUT_LINE('Best student ID = '
|| best_student.id);
DBMS_OUTPUT.PUT_LINE('Best student = ' ||
best_student.first_name || ' '
|| best_student.last_name);
END;
/
Best student ID =
Best student = The Best
(Continued on next part...)
Part:
1
2
3
4
5
6
|