1 Intro To PLSQL
1 Intro To PLSQL
PL/SQL is a block structured language that can have multiple blocks in it. The programs of
PL/SQL are logical blocks that can contain any number of nested sub-blocks. Pl/SQL stands
for "Procedural Language extension of SQL" that is used in Oracle. Although PL/SQL is
closely integrated with SQL language, yet it adds some programming constraints that are not
available in SQL.
The PL/SQL is known for its combination of data manipulating power of SQL with
data processing power of procedural languages. It inherits the robustness, security,
and portability of the Oracle Database.
PL/SQL is not case sensitive so you are free to use lower case letters or upper case
letters except within string and character literals. A line of PL/SQL text contains
groups of characters known as lexical units. It can be classified as follows:
o Delimeters
o Identifiers
o Literals
o Comments
Here, variable_name is a valid identifier in PL/SQL and datatype must be valid PL/SQL
data type. A data type with size, scale or precision limit is called a constrained
declaration. The constrained declaration needs less memory than unconstrained
declaration.
Example:
Radius Number := 5;
Date_of_birth date;
1. DECLARE
2. a integer := 30;
3. b integer := 40;
4. c integer;
5. f real;
6. BEGIN
7. c := a + b;
8. dbms_output.put_line('Value of c: ' || c);
9. f := 100.0/3.0;
10. dbms_output.put_line('Value of f: ' || f);
11. END;
Value of c: 70
Value of f: 33.333333333333333333
o Local Variable: Local variables are the inner block variables which are not accessible
to outer blocks.
o Global Variable: Global variables are declared in outermost block.
1. DECLARE
2. -- Global variables
3. num1 number := 95;
4. num2 number := 85;
5. BEGIN
6. dbms_output.put_line('Outer Variable num1: ' || num1);
7. dbms_output.put_line('Outer Variable num2: ' || num2);
8. DECLARE
9. -- Local variables
10. num1 number := 195;
11. num2 number := 185;
12. BEGIN
13. dbms_output.put_line('Inner Variable num1: ' || num1);
14. dbms_output.put_line('Inner Variable num2: ' || num2);
15. END;
16. END;
17. /
Suppose, you have to write a program which will increase the salary of the
employees upto 30%, you can declare a constant and use it throughout the program.
Next time if you want to increase the salary again you can change the value of
constant than the actual value throughout the program.
1. DECLARE
2. -- constant declaration
3. pi constant number := 3.141592654;
4. -- other declarations
5. radius number(5,2);
6. dia number(5,2);
7. circumference number(7, 2);
8. area number (10, 2);
9. BEGIN
10. -- processing
11. radius := 9.5;
12. dia := radius * 2;
13. circumference := 2.0 * pi * radius;
14. area := pi * radius * radius;
15. -- output
16. dbms_output.put_line('Radius: ' || radius);
17. dbms_output.put_line('Diameter: ' || dia);
18. dbms_output.put_line('Circumference: ' || circumference);
19. dbms_output.put_line('Area: ' || area);
20. END;
21. /
After the execution of the above code at SQL prompt, it will produce the following
result:.
1. Radius: 9.5
2. Diameter: 19
3. Circumference: 59.69
4. Area: 283.53
5.
6. Pl/SQL procedure successfully completed.
PL/SQL Literals
Literals are the explicit numeric, character, string or boolean values which are not
represented by an identifier. For example: TRUE, NULL, etc. are all literals of type
boolean. PL/SQL literals are case-sensitive. There are following kinds of literals in
PL/SQL:
o Numeric Literals
o Character Literals
o String Literals
o BOOLEAN Literals
o Date and Time Literals
PL/SQL comments allow you to describe the purpose of a line or a block of PL/SQL
code.
When compiling the PL/SQL code, the Oracle precompiler ignores comments.
However, you should always use comments to make your code more readable and to
help you and other developers understand it better in the future.
Single-line comments
A single-line comment starts with a double hyphen ( --) that can appear anywhere on
a line and extends to the end of the line.
Multi-line comments
We often use a multi-line comment to describe the purpose of a block of code like
the following example:
/*
This code allow users to enter the customer id and
return the corresponding customer name and credit limit
*/
DECLARE
l_customer_name customers.name%TYPE;
l_credit_limit customers.credit_limit%TYPE;
BEGIN
...
END;
/