Oracle plsql code refactoring - from anonymous block to stored procedure

10
Oracle PL/SQL Refactoring From anonymous block to procedure Carlos Oliveira / July 23, 2014

description

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.

Transcript of Oracle plsql code refactoring - from anonymous block to stored procedure

Page 1: Oracle plsql code refactoring - from anonymous block to stored procedure

Oracle PL/SQL RefactoringFrom anonymous block to procedure

Carlos Oliveira / July 23, 2014

Page 2: Oracle plsql code refactoring - from anonymous block to stored procedure

Agenda

Code Refactoring – From anonymous block to procedure

Introduction Quote Code Refactoring Benefits Next Steps Training & Reference Thank You

Page 3: Oracle plsql code refactoring - from anonymous block to stored procedure

IntroductionI 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.

Page 4: Oracle plsql code refactoring - from anonymous block to stored procedure

“The limits of my language mean the limits of my world.”

Ludwig Wittgenstein

Page 5: Oracle plsql code refactoring - from anonymous block to stored procedure

Code RefactoringScript_text_file_01.sql – file contents below

DECLARE /******************************************************** Description: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 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;/quit

Page 6: Oracle plsql code refactoring - from anonymous block to stored procedure

Code RefactoringCREATE 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.

Page 7: Oracle plsql code refactoring - from anonymous block to stored procedure

BenefitsCompilation

• 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

Page 8: Oracle plsql code refactoring - from anonymous block to stored procedure

Next StepsModularization• 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 …

Page 9: Oracle plsql code refactoring - from anonymous block to stored procedure

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

Page 10: Oracle plsql code refactoring - from anonymous block to stored procedure

Thank you

Carlos Oliveira / July 23, 2014