Oracle PL SQL 5
Oracle PL SQL 5
• It refers to the ability to include SQL statements within a PL/SQL block of code.
• PL/SQL allows for the execution of SQL statements along with the ability to incorporate
programming constructs such as variables, loops, conditionals, and exception handling.
• The key point to note is that the SQL query is embedded within the PL/SQL block using the
appropriate syntax. The result of the SQL query can be stored in PL/SQL variables, used in
conditional statements, or even used to perform DML (Data Manipulation Language)
operations.
• For example the PL/SQL block includes a SQL query to retrieve a value from a table based on
a specific condition. The result of the query is stored in the my_variable PL/SQL variable,
which can then be used within the block for further processing or display.
DECLARE
my_variable VARCHAR2(50);
BEGIN
FROM table_name
WHERE condition;
END;
• Collection of instructions
Example:
• Let's suppose we have a "Customers" table with columns: "ID", "Name", and "Age".
• Inserting data: We can use an INSERT statement to add a new customer into the table.
BEGIN
COMMIT;
END;
Updating data: If we need to change the age of a customer, we can use an UPDATE statement.
BEGIN
UPDATE Customers
SET Age = 30
COMMIT;
END;
Make use of the INTO clause to hold the values returned by a SQL statement
• The INTO clause is used to store the result of a SELECT query into a set of variables or a single
variable.
• It is a way to fetch a single row or a single column value from a SQL query and assign it to a
variable.
• Let's say we have a table called "Employees" with columns "employee_id" and
"employee_name". Suppose we want to retrieve the name of an employee based on their
ID:
DECLARE
BEGIN
SELECT employee_name INTO v_employee_name -- use the INTO clause to store the employee name
FROM Employees
END;
Embedding DML statements in PL/SQL
• Including data manipulation statements such as INSERT, UPDATE, DELETE, or SELECT within a
PL/SQL block or program.
• This allows the PL/SQL program to manipulate or interact with the data in a database.
• For example:
The PL/SQL block first retrieves the salary of an employee with the last name 'Smith' from the
employees table using a SELECT statement. Then, it updates the salary of that employee using an
UPDATE statement. Finally, it prints the updated salary using the DBMS_OUTPUT.PUT_LINE function.
DECLARE
salary employees.salary%TYPE;
BEGIN
FROM employees
UPDATE employees
END;
• Record variable is used to hold a row of data from a table or a query result.
DECLARE
employee_id NUMBER,
first_name VARCHAR2(100),
last_name VARCHAR2(100),
hire_date DATE
);
emp employee_record;
BEGIN
emp.employee_id := 123;
emp.first_name := 'John';
emp.last_name := 'Doe';
emp.hire_date := SYSDATE;
COMMIT;
END;
• First declare a record type called "employee_record" that defines the structure of the record
variable "emp". It has four fields: employee_id (NUMBER), first_name (VARCHAR2),
last_name (VARCHAR2), and hire_date (DATE).