CS322: Database Systems: PL/ SQL
CS322: Database Systems: PL/ SQL
PL/ SQL
DECLARE
v_variable VARCHAR2(5);
BEGIN
SELECT column_name
INTO v_variable
FROM table_name;
EXCEPTION
WHEN exception_name THEN
…
END;
Block Types
Program Constructs
• Assignment operator (: =)
• default keyword
• NOT NULL constraint
If there is a single quotation mark in the string, use a single quotation mark twice
Scalar Data Type
Syntax:
identifier Table.column_name%TYPE;
Declaring Boolean Variables
• Only the values true, false, and null can be assigned to a Boolean
variable.
• The variables are compared by the logical operators and, or, and not.
SET SERVEROUTPUT ON
declare
v_sal number(9,2) := 6000;
BEGIN
v_sal := v_sal/12;
DBMS_OUTPUT.PUT_LINE ('The monthly salary is ' || TO_CHAR(v_sal));
END;
/
Operators
command.
command.
This is
mandatory
Queries must return one and
only one row.
1. Specify the same number of variables in the INTO clause as database columns
in the SELECT clause.
2. Be sure that they correspond positionally and that their data types are
compatible.
3. Use group functions, such as SUM, in a SQL statement, because group functions
apply to groups of rows in a table.
Naming Conventions
BEGIN
INSERT INTO employees
(employee_id, first_name, last_name, email, hire_date, job_id, salary)
VALUES
(employees_seq.NEXTVAL, 'Ruth', 'Cores', 'RCORES', sysdate, 'AD_ASST', 4000);
END;
Updating Data
DECLARE
v_sal_increase employees.salary%TYPE := 800;
BEGIN
UPDATE employees PL/SQL variable assignments
SET salary = salary + v_sal_increase always use : =, and SQL column
WHERE job_id = 'ST_CLERK'; assignments always use =.
END;
DECLARE
v_deptno employees.department_id%TYPE :
BEGIN
DELETE FROM employees
WHERE department_id = v_deptno;
END;
Merging Rows
• Inserts or updates rows in one table, using data from another table.
• Example
DECLARE
v_empno EMPLOYEES.EMPLOYEE_ID%TYPE := 100;
BEGIN
MERGE INTO copy_emp c
USING employees e
ON (c.employee_id = v_empno)
WHEN MATCHED THEN
UPDATE SET
c.first_name = e.first_name,
c.last_name = e.last_name,
c.email = e.email,
Example c.phone_number = e.phone_number,
c.hire_date = e.hire_date,
c.job_id = e.job_id,
c.salary = e.salary,
c.commission_pct = e.commission_pct,
c.manager_id = e.manager_id,
c.department_id = e.department_id
WHEN NOT MATCHED THEN
INSERT VALUES(e.employee_id,
e.first_name, e.last_name, e.email,
e.phone_number, e.hire_date, e.job_id, e.salary,
e.commission_pct, e.manager_id, e.department_id);
END;
Naming Convensions
SQL Cursor
• The Oracle server uses implicit cursors to parse and execute your SQL
statements.
• Using SQL cursor attributes, you can test the outcome of your SQL
statements.
• Delete rows that have the specified employee ID from the employees
table. Print the number of rows deleted.
CASE selector
WHEN expression 1 THEN result 1
WHEN expression 2 THEN result 2
…
WHEN expression N THEN result N
[ELSE resultN+1;]
END;
Example
SET SERVEROUTPUT ON
DEFINE p_grade = a
DECLARE
v_grade CHAR(1) := UPPER('&p_grade' ) ;
v_appraisal VARCHAR2(20);
BEGIN
v_appraisal :=
CASE v_grade
WHEN 'A' THEN 'Excellent'
WHEN 'B' THEN 'Very Good'
WHEN 'C’ THEN 'Good'
ELSE 'No such grade'
END;
DBMS_OUTPUT.PUT_LINE ('Grade: '|| v_grade || ' Appraisal ' || v_appraisal);
END;
Handling Nulls
TRUE TRUE FALSE NULL TRUE TRUE TRUE TRUE TRUE FALSE
FALSE FALSE FALSE FALSE FALSE TRUE FALSE NULL FALSE TRUE
NULL NULL FALSE NULL NULL TRUE NULL NULL NULL NULL
Iterative Control: loop Statements
• Basic loop
• for loop
• WHILE lOOp
Basic Loops
WHILE LOOPS
FOR LOOPS
Guidelines While Using Loops
• Use the basic loop when the statements inside the loop must execute
at least once.
• Use the while loop if the condition has to be evaluated at the start of
each iteration.