Εργαστήριο Ανάπτυξης Εφαρμογών Βάσεων Δεδομένων Εξάμηνο 7 ο
Procedures and Functions Stored procedures and functions are named blocks of code that enable you to group and organize a series of SQL and PL/SQL statements. Both the source code and executable code are stored in the database. the code is in a centralized and accessible location. invoking the stored code is efficient. Stored procedures and functions promote reusability and maintainability. When validated, they can be used in any number of applications. If the requirements change, only the code needs to be updated. Τμ. Ψηφιακών Συστημάτων, Παν. Πειραιώς
CREATE PROCEDURE Statement The CREATE clause enables you to create standalone procedures that are stored in an Oracle database. PL/SQL blocks start with BEGIN, optionally preceded by the declaration of local variables. PL/SQL blocks end with either "END" or "END procedure_name". The REPLACE option indicates that if the procedure exists, it is dropped and replaced with the new version created by the statement. Τμ. Ψηφιακών Συστημάτων, Παν. Πειραιώς
Create Procedure CREATE [OR REPLACE] PROCEDURE procedure_name [(parameter1 [mode] datatype1, parameter2 [mode] datatype2,...)] IS AS [local_variable_declarations; ] BEGIN -- actions; -- SQL statements -- PL/SQL statements END [procedure_name]; Τμ. Ψηφιακών Συστημάτων, Παν. Πειραιώς
Example This procedure will take in the employee ID and the raise amount (expressed as a percentage, such as.06). Based on the raise amount, the salary value in the EMPLOYEES table is updated to the new salary. Τμ. Ψηφιακών Συστημάτων, Παν. Πειραιώς
Example This procedure will take in the employee ID and the raise amount (expressed as a percentage, such as.06). Based on the raise amount, the salary value in the EMPLOYEES table is updated to the new salary. Τμ. Ψηφιακών Συστημάτων, Παν. Πειραιώς
CREATE FUNCTION Statement A function is a named PL/SQL block that can accept parameters, be invoked, and return a value. In general, you use a function to compute a value. You create new functions with the CREATE FUNCTION statement, which may declare a list of parameters and must define the actions to be performed by the standard PL/SQL block. Functions and procedures are structured alike. A function must return a value to the calling environment, whereas a procedure returns zero or more values to its calling environment through OUT parameters. Like a procedure, a function has a header, a declarative section, an executable section, and an optional exception-handling section. A function must have a RETURN clause in the header and at least one RETURN statement in the executable section. Τμ. Ψηφιακών Συστημάτων, Παν. Πειραιώς
Example Create Function Τμ. Ψηφιακών Συστημάτων, Παν. Πειραιώς
Example : Create a function This new function will take in an employee Id and a increment amount as the parameters. The function will return the newly calculated salary amount for the employee Id provided. Τμ. Ψηφιακών Συστημάτων, Παν. Πειραιώς
Handling an exception Τμ. Ψηφιακών Συστημάτων, Παν. Πειραιώς
Creating a package specification CREATE [OR REPLACE] PACKAGE package_name IS AS public type and variable declarations subprogram specifications END [package_name] Example CREATE OR REPLACE PACKAGE hr_pack IS PROCEDURE raise_salary (p_id IN employees.employee_id%type, p_percent IN NUMBER); FUNCTION get_sal (p_id IN employees.employee_id%type) RETURN NUMBER; END hr_pack; Τμ. Ψηφιακών Συστημάτων, Παν. Πειραιώς
Reports create a tabular report to show a list of employees by manager and their salaries. change the report to a chart. Τμ. Ψηφιακών Συστημάτων, Παν. Πειραιώς