Friday, June 26, 2020

How to get the result of your plsql code.

Problem:
======
You want to query the database,
return several columns from one or more tables, 
store them into local variables of a code block for processing.
Rather than placing the values of the columns into
separate variables, you want to create a single variable that contains all the values.


Solution:
======

First create a database record that contains variables that hold data that you want to retrive from the database.A RECORD can hold multiple variables of different datatypes,


DECLARE
TYPE emp_info IS RECORD(first employees.first_name%TYPE,
last employees.last_name%TYPE,
email employees.email%TYPE);
emp_info_rec emp_info;
BEGIN
SELECT first_name, last_name, email
INTO emp_info_rec
FROM employees
WHERE last_name = 'Vargas';
DBMS_OUTPUT.PUT_LINE('The queried employee''s email address is ' || emp_info_rec.email);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee matches the last name provided');
END;

No comments:

Post a Comment