PL SQL Coding Standards
PL SQL Coding Standards
Naming conventions:
T Descriptio Example
ype n
cur Cursor gcur_student
vcr Cursor(var lvcr_student
iable)
tbl Table gtbl_student
rec Record ltrec_address
Primary identifier is the most important part of a name. This can be
a single word or a phrase.
Examples:
Account, Student, StuAddr, LearnerEnroll etc.
T Description Example
ype
i Input only pv_num_ite
parameter ms_i
o Output only pv_sum_o
parameter
io Both input pv_sum_io
and output
Naming Standards
• Tables – Follow identifier naming convention described in the above
section. (scope, primary identifier, modifier)
Ex: HUB_PERSON_CURRENT
• Indexes – table_name_idx##
Ex:HUB_PERSON_CURRENT_IDX01.
• Primary Key constraint – table_name_pk
Ex:HUB_PERSON_CURRENT_PK
• Unique Key constraint - table_name_uk##
Ex:HUB_PERSON_CURRENT_UK
• Foreign Key constraint - table_name _fk##
Ex:HUB_PERSON_CURRENT_UK
• Sequence - table_name_seq## (if associated with a table. Else follows
identifier naming convention from the above section).
Ex:HUB_PERSON_CURRENT_SEQ
• Trigger - table_name_action_trg
Ex: HUB_PERSON_CURRENT_INS_UPD_TRG
• Snapshots (materialized views)- will have the name of the source
object, pre-pended by system name. (Scope, primary identifier,
modifier).
Ex: snapshot of spbpers becomes OASIS_SPBPERS.
• Views – same as tables. (scope, primary identifier, modifier).
Ex:HUB_STUDENT_COURSES
• Function – F_ scope_primary identifier_modifier
Ex : F_OASIS_EXTERNAL_ID
• Procedure - P_ scope_primary identifier_modifier
Ex: P_REG_STUDENT_EXEMPTIONS
• Package - scope_primary identifier_modifier_pkg
Ex : COMMON_DATALOAD_PKG
• Dblink – 2 types –
o 1st type - system name (like OASIS) will point at database in
same state as you are in. in other words, from DWDVLP, OASIS
dblink will point at db: dvlp. In DWTEST same name will point
at pprd. In DWHOUSE same name will point at prod. This
allows code to be promoted without modifying.
o 2nd type, if actual database name, it points at actual
database. For example, in DWDVLP, GEMSPRO dblink will point
at GEMSPRO database, GEMS dblink will point at GEMSDVLP
database. (Note: in some cases these 2 types are the same.
System and db have same name. Those are solved on case by case
basis).
• Insert Script- tablename_ins.sql
Ex : HUB_ROOMS_INS.sql
o The following are the exceptions
For table hub_xref_rule inserts, the script should be named
as xref_kw_qualifier_ins.sql
For table q$error inserts, the insert script should be
named as q$error_catname_ins.sql
SQL
• FORALL
o Use with inserts, updates and deletes.
o Move data from collections to tables.
For Ex: the For loop has more DB hits for every execution
PL/SQL
• If your program could be useful on both the server side and the
client side, move it to the server, because it can be invoked from
both sides there.
• If the program is used only in and relevant to client-side modules
(it may, for example, manipulate the contents of a field in a form)
and you think or know that it will be useful in more than one module,
put it into a shared library.
• If your program is very specific to a current form or report, define
it within that module.
• Use small, narrowly-focused packages. For example, one package for
student matching, another for creating person records. Package is
usually created for the following reasons
o Logical containers for related elements
o Overloading
o Package-level data and caching
o Initialization section
o Local or nested modules
o To keep executable sections small/tiny
• Each variable and constant you declare should have one purpose and
one purpose only in an object (PKG, FUNCTION, and PROCEDURE). The
name for that variable or constant should describe, as clearly as
possible, that single-minded purpose ( Use the identifier naming
convention).
• Always Fetch into Cursor Records. Main advantages are less code and
if your select list changes your code is not affected.
o For ex The below ( highlighted in yellow) is correct but the ex
in red is more efficient.
name VARCHAR2 (30);
minbal NUMBER(10,2);
BEGIN
OPEN company_pkg.allrows;
FETCH company_pkg.allrows
INTO name, minbal;
IF name = ‘ACME’ THEN ...
CLOSE company_pkg.allrows;
rec company_pkg.allrows%ROWTYPE;
BEGIN
OPEN company_pkg.allrows;
FETCH company_pkg.allrows INTO rec;
IF rec.name = ‘ACME’ THEN ...
• Place the keywords (IF, THEN, ELSE, ELSIF, ENDIF) on separate lines.
Avoid Unnecessary Nested IFs
For Ex: The following statements are equivalent. The flat structure
expresses the logic more clearly and with less code.
Nested Flat
IF <condition1> IF <condition1>
THEN THEN
... …
ELSE ELSIF <Condition2>
IF <Condition2> THEN
THEN …
… ELSIF <Condition3>
ELSE THEN
IF <Condition3> …
THEN ELSIF <Condition4>
… THEN
ELSE …
IF <Condition4> END IF;
THEN
…
END IF;
END IF;
END IF;
END IF;
IF eligible_for_register (cat_rec.crnno)
THEN
give_waiver (cat_rec.pidm);
END IF;
Avoid IF With Boolean Variables
IF hiredate < SYSDATE
THEN
date_in_past := TRUE;
ELSE
date_in_past := FALSE;
END IF;
With this:
date_in_past :=
hiredate < SYSDATE;
• Avoid Unstructured Exits from Loops
o Do not EXIT or RETURN out of a FOR loop.
o A FOR loop should only be used when you want to execute the body
a fixed number of times.
o Stay for the duration or use a different loop construct.
o Do not use the EXIT syntax in a WHILE loop.
o The loop should be terminated only when the condition in the
boundary evaluates to FALSE.