There Is a Better Way
Learning PL/SQL is not so difficult. With a little effort and a willingness to expand your programming tool set, PL/SQL skills can be acquired with relative ease. In fact, quickly learning new programming languages is a key skill for all developers. It’s also an interesting challenge and helps make for a more marketable skill set.
My advice to any developer who has a chance to learn PL/SQL is to seize the opportunity. The effort required to learn this interesting and useful language far outweighs the energy required to avoid it in favor of a more complex and potentially brittle mechanism. Your organization and its data users will thank you for creating more resilient solutions, and you’ll thank yourself when your newfound PL/SQL skills turn out to be reusable in other project work.
Let’s start with a few examples of PL/SQL just to see how easily the language can be learned. The code examples will be revisited later on to illustrate how to migrate them to a more resilient form. One last point in relation to looking at code examples: the ability to read code is one of the great skills in software development and integration. We’ll be looking to enhance this skill as the examples unfold.
Gaining a Basic Understanding of PL/SQL
More experienced PL/SQL readers can skip this section. It’s included to provide PL/SQL beginners or novices with an indication that the language can be readily understood purely on the basis of its similarity to mainstream, high-level programming languages. This content would have helped me when I first started learning PL/SQL. In the next section, you’ll begin the journey of learning to read PL/SQL written by someone else.
How to Read Existing or Legacy PL/SQL Code
Let’s now finally take a look at our very first piece of PL/SQL, in Example 1-1. What do you reckon this block of code is doing? I’ve added a few extra comments to assist the reader.
Example 1-1. Introducing PL/SQL
DECLARE
x
NUMBER
:
=
100
;
BEGIN
FOR
i
IN
1
..
20
LOOP
— A for loop similar to Java
IF
MOD
(
i
,
2
)
=
0
THEN
— if i is even
INSERT
INTO
temp
VALUES
(
i
,
x
,
‘i is even’
);
ELSE
INSERT
INTO
temp
VALUES
(
i
,
x
,
‘i is odd’
);
END
IF
;
x
:
=
x
+
100
;
END
LOOP
;
COMMIT
;
END
;