PLSQL Lecture
PLSQL Lecture
Programming
PL/SQL and
Embedded SQL
PL/SQL
DECLARE
number1 NUMBER(2);
number2 number1%TYPE := 17; -- value default
text1 VARCHAR2(12) := 'Hello world';
text2 DATE := SYSDATE; -- current date and time
BEGIN
SELECT street_number INTO number1
FROM address
WHERE name = 'INU';
END;
Functions
IN Parameter
Used as input only
Passed by reference
OUT Parameter
initially NULL.
is assigned a value that is returned to the calling program
IN OUT Parameter
may or may not have initial value
That initial value may or may not be modified by the called
program.
Any changes made to the parameter are returned to the calling
program by default by copying but - with the NOCOPY hint - may
be passed by Reference
Pipelined Table Function
Database Applications
Architecture
Oracle Server
Database
Benefits of Stored Procedures I
Security
Control data access through procedures and functions.
E.g. grant users access to a procedure that updates a table,
but not grant them access to the table itself.
Performance
The information is sent only once between database and
application and thereafter invoked when it is used.
Network traffic is reduced compared with issuing individual
SQL statements or sending the text of an entire PL/SQL block
A procedure's compiled form is readily available in the
database, so no compilation is required at execution time.
The procedure might be cached
Benefits of Procedures II
Memory Allocation
Stored procedures take advantage of the shared memory
capabilities of Oracle
Only a single copy of the procedure needs to be loaded into
memory for execution by multiple users.
Productivity
By designing applications around a common set of
procedures, you can avoid redundant coding and increase
your productivity.
Procedures can be written to insert, update, or delete rows
from a table and then called by any application without
rewriting the SQL statements necessary to accomplish these
tasks.
If the methods of data management change, only the
procedures need to be modified, not all of the applications
that use the procedures.
Benefits of Procedures III
Integrity
Stored procedures improve the integrity and
consistency of your applications. By developing all of
your applications around a common group of
procedures, you can reduce the likelihood of
committing coding errors.
You can test a procedure or function to guarantee that
it returns an accurate result and, once it is verified, reuse
it in any number of applications without testing it again.
If the data structures referenced by the procedure are
altered in any way, only the procedure needs to be
recompiled; applications that call the procedure do not
necessarily require any modifications.
Packages
A method of encapsulating and storing related
procedures, functions, variables, cursors and other
package constructs together as a unit in the
database for continued use as a unit.
Similar to standalone procedures and functions,
packaged procedures and functions can be called
explicitly by applications or users.
Organize routines
Increased functionality (e.g. global package
variables can be declared and used by any
procedure in the package) and
Increased performance (e.g. all objects of the
package are parsed, compiled, and loaded into
memory once).
Package Manage Tasks in Database
Database applications
Program code
.
FIRE_EMP(…) explicitly call packaged
EMP_MGMT.FIRE_EMP(…);
BEGIN
.
procedures as necessary.
Program code . After being granted the
.
EMP_MGMT.HIRE_EMP(…);
END; privileges for the
. package, a user can
Program code HIRE_EMP(…) explicitly execute any of
BEGIN the procedures
. contained in it.
Program code .
. END; EXECUTE marks_mgmt.
EMP_MGMT.HIRE_EMP(…); credit_labmark(99234,’CS2
Program code SAL_RAISE(…) 312’,20)
. BEGIN
EMP_MGMT.SAL_RAISE(…); . Packages offer several
. . development and
Program code END; performance
advantages over
standalone stored
Database procedures;
Benefits of Packages
Encapsulation of related procedures and variables providing:
Better organization during the development process and
for granting privileges
Declaration of public and private procedures, variables,
constants, and cursors
Better performance
An entire package is loaded into memory when a
procedure within the package is called for the first time in
one operation, as opposed to the separate loads required
for standalone procedures. When calls to related
packaged procedures occur, no disk I/O is necessary to
execute the compiled code already in memory.
A package body can be replaced and recompiled without
affecting the specification. Objects that reference a
package's constructs (always via the specification) need
not be recompiled unless the package specification is also
replaced. Unnecessary recompilations can be minimized,
so in less impact on overall database performance.
Triggers vs Procedures and Packages
Triggers are similar to stored procedures. A trigger can
include SQL and PL/SQL statements to execute as a unit
and can invoke stored procedures. Triggers are stored
in the database separate from their associated tables.
Procedures and triggers differ in the way that they are
invoked.
A procedure is explicitly executed by a user,
application, or trigger.
Triggers (one or more) are implicitly fired (executed)
by Oracle when a triggering INSERT, UPDATE, or
DELETE statement is issued, no matter which user is
connected or which application is being used.
Retrieval: Impedance Mismatch
What happens when the query returns several rows? The
host variables can only hold one value.
Oracle will only pass the first row returned by the query to
the PL/SQL block (or host language program).
Re-executing the SELECT operation will only run the query
again and so the first row will be selected again.
Different type systems
Different execution models
Cursors
host variable
…
EXEC SQL SELECT labmark INTO :labmark FROM enrol
main()
{ EXEC SQL WHENEVER SQLERROR DO sqlerror()
EXEC ORACLE OPTION (ORACA=YES);
oraca.orastxtf = 1; Oracle_Connect(); printf("Connected to
Oracle\n");
Cursor for query
EXEC SQL DECLARE studcursor CURSOR FOR
SELECT s.name, e.courseno, e.labmark,
FROM student s, enrol e WHERE s.studno = e.studno;
Do the query
EXEC SQL OPEN studcursor; printf(”Name/Course/LabMark\n");
Loop to fetch rows
while (sqlca.sqlcode == 0) {
EXEC SQL FETCH studcursor
INTO :studname, :cno, :labmark
printf("%s,%s,%d", studname, cno, labmark);
}
printf("%ld rows selected.\n",sqlca.sqlerrd[2]);
EXEC SQL CLOSE studcursor;
EXEC SQL COMMIT WORK RELEASE;
exit(1);}
Examples of Packages and
Procedures
Create Package Specification
PROCEDURE apply_marks;
end marks_mgmt;
Create Package Body
CREATE PACKAGE BODY marks_mgmt AS
new_status CHAR(20); /* Global variable to record status of
transaction being applied. Used for update in enter_marks. */
EXCEPTION
WHEN NO_DATA_FOUND THEN
/* Create new enrolment if not found */
INSERT INTO enrol (studno, courseno, labmark, exammark)
VALUES(sno, cno, credit, null);
do_journal_entry(sno, cno, 'N');
WHEN mark_overflow THEN
new_status := ’Mark Overflow’;
WHEN OTHERS THEN
/* Return other errors to application */
new_status := 'Error: ' || SQLERRM(SQLCODE);
END credit_labmark;