You are on page 1of 4

Database Notes

- Header: name to the program unit and also identifies it as a procedure, a


function, or a package; it can also identify any parameters.
- Declarative: this is used to define variables, constants, cursors, and
exceptions. This section can be used with a keyword IS or AS.
- Executable: This is the main processing area; starts with the keyword BEGIN
and ends with the keyword END.
- Exception (handler): This section is optional; it deals with error handling
and starts with the keyword EXCEPTION.
- Variable: a structure used to store data during a PL/SQL block, subprograms,
and package execution. Declaration of a variable allocates storage space on
memory for a value; they also specify the type of the variable and the name
so that the space can be referenced. We can also assign a value at
declaration such as a constant value, NOT NULL, or default.
- Constant: a variable whose value never changes; it is declared in the
declaration section, which is before the executable section.
- 3 types of parameter modes:
1. IN: means that a value is being passed into a subprogram.
2. OUT: indicates that the subprogram is passing a value generated within it
out to the calling environment
3. IN OUT: means that a value is being passed into a subprogram; and this
may be changed so that the value is passed out to the calling
environment.
- Functions: must execute a RETURN statement; are named or called
differently than procedures. They are usually called within another command,
whereas procedures are called as statements.
- Package: it has two sections, the body and the specification. The package
specification is the only part required; it can exist without a body. Whereas,
the body will not be valid without the specification.
- The INTO clause is required in a SELECT statement of s PL/SQL program
- RECORD: a composite data structure; it has components that can be
manipulated individually. This is used to treat dissimilar data as a logical unit.
- The flow logic: of a PL/SQL block can be changed by using a number of
control structures.
- Branching logic: it is implemented either using IF statement, or the CASE
statement.
- These statements are sometimes referred to as expressions
Labs:
1.
a. CREATE OR REPLACE PROCEDURE add_job
(p_job_id IN jobs.job_id%TYPE,
p_job_title IN jobs.job_title%TYPE)
IS
BEGIN
INSERT INTO jobs(job_id, job_title) VALUES(p_job_id, p_job_title);
END add_job;

b. EXECUTE add_job('IT_DBA', 'Database Administrator');


COMMIT;

c. EXECUTE add_job('ST_MAN', 'Stock Manager');

Since the primary key job.job_ids unique constraint is violated an error


is brought up. The job_id ST_MAN already exists, therefore this specific
ID cannot be recorded.

1.
a) DECLARE
id employees.employee_id%TYPE;
name employees.last_name%TYPE;
salary employees.salary%TYPE;
BEGIN
id:= 178;
query_emp (id, name, salary);
DBMS_OUTPUT.PUT_LINE ('id: ' || id);
DBMS_OUTPUT.PUT_LINE ('name: ' || name);
DBMS_OUTPUT.PUT_LINE ('salary: ' || salary);
END;
/

b) The significance of the supplied value in the anonymous block is that the
IN parameter has to provide the procedure with a value. Thus, the variable
must be initialized before it is used as a parameter.

c) Emp_name & emp_sal do not hold any values until the procedure is called.
Afterwards, theyll hold the values returned by the procedure since it
contains the two parameters as OUT modes.

d) VARIABLE id employees.employee_id%TYPE;
VARIABLE salary employees.salary%TYPE;
VARIABLE name employees.last_name%TYPE;
BEGIN
:id := 178;
query_emp (:id, :name, :salary);
DBMS_OUTPUT.PUT_LINE ('id: ' || :id);
DBMS_OUTPUT.PUT_LINE ('name: ' || :name);
DBMS_OUTPUT.PUT_LINE ('salary: ' || :salary);
END;
/

2. CREATE OR REPLACE PROCEDURE format_phone


(phone_number IN OUT VARCHAR2) IS
BEGIN
phone_number := '(' || SUBSTR(phone_number, 1, 3) ||
')' || SUBSTR(phone_number, 4, 3) ||
'-' || SUBSTR(phone_number, 7);
END format_phone;

Lab 6

1.
a) CREATE OR REPLACE FUNCTION valid_deptid
(p_deptid IN departments.department_id%TYPE)
RETURN BOOLEAN
IS
v_dummy VARCHAR2(1);
BEGIN
SELECT 'x'
INTO v_dummy
FROM departments
WHERE department_id = p_deptid;
RETURN (TRUE);
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN (FALSE);
END valid_deptid;
/

b) PROCEDURE ADD_EMPLOYEE
(p_lname employees.last_name%TYPE,
p_fname employees.first_name%TYPE,
p_email employees.email%TYPE,
p_job employees.job_id%TYPE DEFAULT 'SA_REP',
p_mgr employees.manager_id%TYPE DEFAULT 145,
p_sal employees.salary%TYPE DEFAULT 1000,
p_comm employees.commission_pct%TYPE DEFAULT 0,
p_deptid employees.department_id%TYPE DEFAULT 30)
IS
BEGIN
IF valid_deptid(p_deptid) THEN
INSERT INTO employees(employee_id, last_name, first_name,
email, job_id, manager_id, hire_date,
salary, commission_pct, department_id)
VALUES (employees_seq.NEXTVAL, p_lname, p_fname, p_email,
p_job, p_mgr, TRUNC (SYSDATE, 'DD'), p_sal,
p_comm, p_deptid);
ELSE
RAISE_APPLICATION_ERROR (-20204,
'Wrong department ID. Please, Try again.');
END IF;
END ADD_EMPLOYEE;

c) EXECUTE new_emp(p_lname=>'Harris', p_fname=>'Jane',


p_email=>'JAHARRIS', p_deptid => 15)

Result:
ERROR at line 1;
ORA-20204: Invalid department ID. Try again.
ORA-06512: at PLSQL.ADD_EMPLOYEE, line 18
ORA-06512: at line 1

d) EXECUTE new_emp(p_lname=>'Harris',p_fname=>'Joe',
p_email=>'JOHARRIS',p_deptno => 80)

Result: PL/SQL Procedure successfully completed

2. CREATE OR REPLACE FUNCTION calc_tax (SALARY NUMBER) RETURN


NUMBER IS BEGIN RETURN (SALARY * 0.08);
END;
SELECT employee_id, last_name, salary, calc_tax(salary)
FROM employees;

You might also like