نسخة Ch3-Procedural Language SQL - Copy-last
نسخة Ch3-Procedural Language SQL - Copy-last
وزارة التعليم
جامعة االمام محمد بن سعود اإلسالمية
الكلية التطبيقية
Database programming
Prepared by : Marwan Ali
1. Procedural Capabilities:
PL/SQL extends SQL by adding procedural constructs such as conditional
statements, loops, and exception handling, allowing developers to write more
complex and structured programs.
2. Block Structure:
PL/SQL programs are organized into blocks, which can include declarations,
executable statements, and exception handlers. The block structure makes it
easy to manage and structure code.
PL/SQL features
4. Control Structures:
PL/SQL includes standard control structures such as IF-THEN-ELSE
statements, CASE statements, and various loop constructs (FOR, WHILE,
and LOOP).
PL/SQL features
5. Exception Handling:
4. PL/SQL provides a robust exception handling mechanism to deal with errors and unexpected
events. Developers can define exception handlers to gracefully manage errors and ensure the
integrity of the application.
7. Packages:
Packages in PL/SQL enable the encapsulation of related procedures, functions, and
variables into a single unit. This helps in organizing and managing code in a more
structured manner.
8. Triggers:
Triggers are special types of stored procedures that automatically execute in
response to specific events, such as INSERT, UPDATE, or DELETE
operations on a table. Triggers are useful for enforcing business rules and
maintaining data integrity.
9. Dynamic SQL:
PL/SQL supports dynamic SQL, allowing developers to construct and
execute SQL statements dynamically at runtime. This feature
PL/SQL features
11. Collections:
o PL/SQL supports collections, including associative
arrays, nested tables, and VARRAYs. Collections
provide a convenient way to work with sets of data.
PL/SQL features
12. Security:
o PL/SQL provides security features, such as privileges and roles,
to control access to database objects and ensure data security.
Control Structures: Conditional and iterative statements for controlling the flow of
program execution.
Cursors: Enable the processing of result sets returned by SQL queries.
Declare (optional)
• Variables, cursor ,user-defined exceptions
Begin (Mandotary)
• SQL statements
• PL/SQL statements
Exception(optional)
• Action to perform when error occurred
End; (Mandotary)
/
example
Block Types
v_name VARCHAR2(50);
v_age NUMBER; VARCHAR2: Variable-length character string.
v_birthdate DATE;
v_is_adult BOOLEAN := TRUE; -- Initialization NUMBER: Numeric data type.
BEGIN -- Your code here DATE: Date and time data type.
You can assign values to variables using the := assignment operation
DECLARE
v_name VARCHAR2(50);
BEGIN
v_name:=‘John Doe’; -- Assigning a value to the variable
-- your code here
END;
/
:Variables
2- Composite Types:
Type%: The %Type datatype is use to define the variable as column name datatype for
specified table
For example, assume there is a column named title in a table named books.
To declare a variable named my_title that has the same datatype as column
title, use dot notation and the %TYPE attribute, as follows:
my_title books.title%TYPE;
Example: display employee name, job, salary from
emp table where employee number = 7782
using %type
Rowtype% attribute
Rowtype%:
In PL/SQL, records are used to group data. A record consists of a number of
related fields in which data values can be stored. The %ROWTYPE attribute
provides a record type that represents a row in a table. The record can store
an entire row of data selected from the table
pl/sql code to display employee name, job, salary from emp table where
employee number = 7782
Example: display employee name, job, salary from emp table where employee number =
7782
using %rowtype
1- declare x variable take the same data type from employee table (emp)
2- in line 8 When printing a sentence with a value: variable name then (.)
then column name in the table:
DBMS_OUTPUT_LINE('Name' || x.ename);
Print statement in pl/sql
DBMS_OUTPUT.PUT_LINE( )
write what you want to print in parentheses
Example:
1- DBMS_OUTPUT.PUT_LINE(‘Eman’); output: Eman
2- DBMS_OUTPUT.PUT_LINE(10); output: 10
3- x:=14
DBMS_OUTPUT.PUT_LINE(x); output: 14
Operations in PL/SQL
A. Arithmetic Operations
• Addition (+), Subtraction (-), Multiplication (*), Division (/), Modulus (%)
B. Comparison Operations
• Equal to (=), Not equal to (<>), Greater than (>), Less than (<), Greater than
or equal to (>=), Less than or equal to (<=)
C. Logical Operations
• AND, OR, NOT
The PL/SQL Comments
DECLARE
-- variable declaration
message varchar (20) := ‘ Hello World ‘ ;
BEGIN
/*
*
*/
dbm.output.put_line(‘message’);
END
:Single row function
• Single-row functions are built-in functions and include character, numeric, date,
conversion and user-defined functions.
• All single row functions can be used in SQL or PL/SQL programs and can be used in
the SELECT, WHERE and ORDER BY clauses.
Conversion functions
this example convers a string to a number value using the to_number function
DECLARE
v_string_number VARCHAR2(10) := '123’;
v_numeric_number NUMBER;
BEGIN
v_numeric_number := TO_NUMBER(v_string_number);
DBMS_OUTPUT.PUT_LINE('Numeric Number: ' || v_numeric_number);
END;
/
To_date
This example converts a string to a date using the to_date function. The second argument
.specifies the format of the input string
DECLARE
v_date_string VARCHAR2(20) := '2023-01-01';
v_date_value DATE;
BEGIN v_date_value := TO_DATE(v_date_string, 'YYYY-MM-DD');
DBMS_OUTPUT.PUT_LINE('Date Value: ' || TO_CHAR(v_date_value, 'DD-MON-YYYY’));
END;
/
To_char
This example convers a numeric value to a string using the to_char function
DECLARE
v_numeric_value NUMBER := 123.45;
v_string_value VARCHAR2(20);
BEGIN
v_string_value := TO_CHAR(v_numeric_value);
DBMS_OUTPUT.PUT_LINE('String Value: ' || v_string_value);
END;
/
PL/SQL Nested Block
END add_numbers;
/
:Example
In the above example:
•The procedure is named add_numbers.
•It takes two parameters (p_number1 and P_number2) of type NUMBER.
•Inside the procedure, it calculates the sum of the two numbers and
stores it in the variable v_sum.
•It then prints the result using the DBMS_OUTPUT.PUT_LINE statement.
After creating the procedure, you can call it using a PL/SQL block:
-- Call the procedure
BEGIN
add_numbers(10, 20);
END;
/
PL/SQL Insert into
-- Create a simple PL/SQL procedure for inserting an employee without commit
CREATE OR REPLACE PROCEDURE insert_employee (
p_employee_id NUMBER,
p_first_name VARCHAR2,
p_last_name VARCHAR2,
p_salary NUMBER
)
IS
BEGIN
-- Insert the new employee record
INSERT INTO employees(employee_id, first_name, last_name, salary)
VALUES (p_employee_id, p_first_name, p_last_name, p_salary);
DBMS_OUTPUT.PUT_LINE('Employee inserted successfully without commit.’);
END insert_employee;
/
Selection
DECLARE
v_employee_id NUMBER := 2;
v_new_salary NUMBER := 72000;
BEGIN
update_employee_salary(v_employee_id, v_new_salary);
END;
/
Update