How to move your Oracle PL/SQL code from an anonymous block in a file to a database stored object. In a simple procedure you achieve huge gains in security, performance, object reuse, and more. With a small enhancement comes a great financial cost reduction and productivity increase.
2. Agenda
Code Refactoring – From anonymous block to
procedure
Introduction
Quote
Code Refactoring
Benefits
Next Steps
Training & Reference
Thank You
3. Introduction
I am a forward-looking Information Systems Architect with a
solid Oracle DBA background comprising the daily
infrastructure tasks of the DBA, several projects as a Data
Modeler, and performance management projects.
I Started on the mainframe business, and soon had a deep dive
in application development for Oracle databases. After
acquiring an Oracle certification, I worked on performance
enhancement for applications using Oracle databases, and later
worked several years as an infrastructure DBA, later I worked
on data modeling projects and more recently a performance
management project, on both application and database layers.
4. “The limits of my language
mean the limits of my world.”
Ludwig Wittgenstein
6. Code Refactoring
CREATE OR REPLACE PROCEDURE DB_USER.PROC_NAME IS
/********************************************************
Description: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Changed: xx/xx/xxxx
Changed By: xxxxxxx
********************************************************/
v_msg_text VARCHAR2(4000);
v_msg_num NUMBER(10) := 0;
BEGIN
v_msg_text := 'Procedure executed';
v_msg_num := 1;
END;
/
Script_text_file_01.sql new
BEGIN
DB_USER.PROC_NAME;
END;
/
quit
Since the code is now stored in
the database, the
Script_text_file_01.sql only
contains the call to the
procedure.
7. Benefits
Compilation
• Most of the code is already compiled – only caller script remains
Memory
• Reduced memory usage
• Faster load
Tracking
• Code can be tracked inside DB
• Explicit dependency – change in related objects are flagged
Reuse
• Code is ready to be reused
• Change in central stored object is immediate for all calling scripts
Security
• Code can be included in database backup/restore
• Access to code restricted by database privileges
8. Next Steps
Modularization
• Identification of common code for grouping, organization or creation of internal
or even external procedures
• Encapsulation of complex code in procedures of functions
Error Control
• Inclusion of an Exception section for each code block
• Use of user-created exceptions
• Creation of common exception procedures
Best Practices
• Variable initialization
• Eliminate hard-code
And much more …
9. Training
• Performance Tuning Guide and Reference
http://docs.oracle.com/cd/B10500_01/server.920/a96533/toc.htm
• SQL Reference
http://docs.oracle.com/cd/B10500_01/server.920/a96540/toc.htm
• PL/SQL User's Guide and Reference
http://docs.oracle.com/cd/B10500_01/appdev.920/a96624/toc.htm
Resources at Oracle website