Unit 1 (ADBC)
Unit 1 (ADBC)
SYCS SEM-3
Unit- 1
Overview of PL/SQL
PL/SQL stands for “Procedural Language extensions to the Structured Query Language”.
SQL is a popular language for both querying and updating data in the relational database management systems
(RDBMS).
PL/SQL is an embedded language. PL/SQL only can execute in an Oracle Database. It was not designed to use as a
standalone language like Java, C#, and C++.
In other words, you cannot develop a PL/SQL program that runs on a system that does not have an Oracle Database.
Advantages of PL/SQL
Declare
Declaration Statement;
SQL Lorem ipsum dolor sit amet, consectetur
SQL Statement
adipiscing elit, sed do eiusmod tempor. Ipsum
executor
Begin
- From an expert dolor sit amet elit, sed do eiusmod tempor.
SQL statements; Procedural
Procedural statements;
statements
Exception
Exception handling
statement;
PL/SQL
END Statement
executor
PL/SQL Block PL/SQL Engine Database Server
Database Server
Declare
Declaration Statement;
SQL Lorem ipsum dolor sit amet, consectetur
SQL Statement
adipiscing elit, sed do eiusmod tempor. Ipsum
executor
Begin
- From an expert dolor sit amet elit, sed do eiusmod tempor.
SQL statements; Procedural
Procedural statements;
statements
Exception
Exception handling
statement;
PL/SQL
END Statement
executor
PL/SQL Block PL/SQL Engine Database Server
Database Server
Declare
Declaration Statement;
SQL Lorem ipsum dolor sit amet, consectetur
SQL Statement
adipiscing elit, sed do eiusmod tempor. Ipsum
executor
Begin
- From an expert dolor sit amet elit, sed do eiusmod tempor.
SQL statements; Procedural
Procedural statements;
statements
Exception
Exception handling
statement;
PL/SQL
END Statement
executor
PL/SQL Block PL/SQL Engine Database Server
Database Server
Declare
Declaration Statement;
SQL Lorem ipsum dolor sit amet, consectetur
SQL Statement
adipiscing elit, sed do eiusmod tempor. Ipsum
executor
Begin
- From an expert dolor sit amet elit, sed do eiusmod tempor.
SQL statements; Procedural
Procedural statements;
statements
Exception
Exception handling
statement;
PL/SQL
END Statement
executor
Architecture of PL/SQL:
Declare
Declaration Statement;
SQL Lorem ipsum dolor sit amet, consectetur
SQL Statement
adipiscing elit, sed do eiusmod tempor. Ipsum
executor
Begin
- From an expert dolor sit amet elit, sed do eiusmod tempor.
SQL statements; Procedural
Procedural statements;
statements
Exception
Exception handling
statement;
PL/SQL
END Statement
executor
Fundamentals of PL/SQL
Like other programming languages, PL/SQL has a character set, reserved words, punctuation,
datatypes, and fixed syntax rules.
PL/SQL programs are written as lines of text using a specif ic set of characters:
■ U p p e r - a n d l o w e r - c a s e l e t t e r s A . . Z a n d a . . z
■ N u m e r a l s 0 . . 9
■ Delimiters (simple and compound symbols) – A delimiter is a simple or compound symbol that has a
special meaning to PL/SQL. For example, you use delimiters to represent arithmetic operations such as
addition and subtraction.
■ Comments – The PL/SQL compiler ignores comments, but you should not.
Adding comments to your program promotes readability and aids
understanding. Generally, you use comments to describe the purpose and use
of each code segment. PL/SQL supports two comment styles: single-line and
multi-line
Declarations
This section starts with the keyword DECLARE. It is an optional section and def in es all variables, cursors,
subprograms, and other elements to be used in the program.
Executable Commands
This section is enclosed between the keywords BEGIN and END and it is a
mandatory section. It consists of the executable PL/SQL statements of the program.
It should have at least one executable line of code, which may be just a NULL
command to indicate that nothing should be executed.
Exception Handling
This section starts with the keyword EXCEPTION. This optional section contains
( )
What is Identifiers
Identifiers are nothing but a name that is given to a PL/SQL object. Each Identifier has some
Properties of Identifiers
● Identifiers Can contain dollar sign (‘$’), underscore (‘_’) and hash sign (‘#’)
Here, variable_name is a valid identif ier 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;
Naming rules for PL/SQL variables
The variable in PL/SQL must follow some naming rules like other programming languages.
○ Variable name should not be the same as the table table's column of that block.
○ The name of the variable must begin with ASCII letter. The PL/SQL is not case sensitive so it could be either
lowercase or uppercase. For example: v_data and V_DATA refer to the same variables.
○ You should make your variable easy to read and understand, after the first character, it may be any number,
● Once the variable is declared, they are ready to hold the data of defined type.
● The values of these variables can be assigned either in execution section or at the
● Once a particular value has been assigned, it will be stored in the allocated memory
○ Forward references are not allowed i.e. you must declare a constant or variable before referencing it in
The first declaration is illegal because the TOTAL variable must be declared before using it in an
assignment expression.
○ Variables belonging to the same datatype cannot be declared in the same statement.
It is an illegal declaration.
Example of initializing variable
Let's take a simple example to explain it well:
1. DECLARE
2. a integer := 30;
3. b integer := 40; After the execution, this will produce the following result:
4. c integer; Value of c: 70
5. f real;
Value of f: 33.333333333333333333
6. BEGIN
7. c := a + b;
8. dbms_output.put_line('Value of c: ' || c);
PL/SQL procedure successfully completed.
9. f := 100.0/3.0;
10. dbms_output.put_line('Value of f: ' || f);
11. END;
Variable Scope in PL/SQL:
PL/SQL allows nesting of blocks. A program block can contain another inner block. If you declare a variable within an
inner block, it is not accessible to an outer block. There are two types of variable scope:
○ Local Variable: Local variables are the inner block variables which are not accessible to outer blocks.
1. DECLARE
2. -- Global variables After the execution, this will produce the following result:
3. num1 number := 95;
4. num2 number := 85;
Outer Variable num1: 95
5. BEGIN
Outer Variable num2: 85
6. dbms_output.put_line('Outer Variable num1: ' || num1);
7. dbms_output.put_line('Outer Variable num2: ' || num2);
Inner Variable num1: 195
8. DECLARE
9. -- Local variables Inner Variable num2: 185
10. num1 number := 195;
11. num2 number := 185; PL/SQL procedure successfully completed.
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. /
control statements:
A control statement allows structured control of the sequence of application statements, such
as loops, and conditional tests. It also allows switching the input stream to another file, opening
and closing files, and various other operations.
IF-THEN Statement
● It is the simplest form of the IF control statement, frequently used in decision-making and
changing the control flow of the program execution.
● The IF statement associates a condition with a sequence of statements enclosed by the
keywords THEN and END IF.
● If the condition is TRUE, the statements get executed, and if the condition is FALSE or NULL, then
the IF statement does nothing.
Let us try an example that will help you understand the concept −
DECLARE
a number(2) := 10;
BEGIN a:= 10;
-- check the boolean condition using if statement
IF( a < 20 )
THEN
-- if condition is true then print the following
dbms_output.put_line('a is less than 20 ‘ );
END IF;
dbms_output.put_line('value of a is : ' || a);
END;
/
IF-THEN-ELSE Statement
● The PL/SQL loops are used to repeat the execution of one or more
statements for specif ie d number of times. These are also known as
iterative control statements.
PL/SQL While Loop
● PL/SQL while loop is used when a set of statements has to be executed as
long as a condition is true, the While loop is used.
● The condition is decided at the beginning of each iteration and continues
until the condition becomes false.
● Note: You must follow these steps while using PL/SQL WHILE Loop.
• Initialize a variable before the loop body.
• Increment the variable in the loop.
• You can use EXIT WHEN statements and EXIT statements in While loop but
it is not done often.
1. DECLARE
2. i INTEGER := 1;
3. BEGIN
4. WHILE i <= 10 LOOP
5. DBMS_OUTPUT.PUT_LINE(i);
6. i := i+1;
7. END LOOP;
8. END;
● /
1. DECLARE
2. VAR1 NUMBER;
3. VAR2 NUMBER;
4. BEGIN
5. VAR1:=200;
6. VAR2:=1;
7. WHILE (VAR2<=10)
8. LOOP
9. DBMS_OUTPUT.PUT_LINE (VAR1*VAR2);
10. VAR2:=VAR2+1;
11. END LOOP;
12. END;
GOTO Statement
The GOTO statement allows you to transfer control to a labeled block or statement.
The following illustrates the syntax of the GOTO statement:
GOTO label_name;
The label_name is the name of a label that identifies the target statement. In the
program,
you surround the label name with double enclosing angle brackets as shown below:
<<label_name>>;
When PL/SQL encounters a GOTO statement, it transfers control to the first executable
statement after the label.
BEGIN
GOTO second_message;
<<first_message>>
DBMS_OUTPUT.PUT_LINE( 'Hello' );
GOTO the_end;
<<second_message>>
DBMS_OUTPUT.PUT_LINE( 'PL/SQL GOTO Demo' );
GOTO first_message;
<<the_end>>
DBMS_OUTPUT.PUT_LINE( 'and good bye...' );
END;
DECLARE
a number(2) := 30;
BEGIN
<<loopstart>>
-- while loop execution
WHILE a < 50 LOOP
dbms_output.put_line ('value of a: ' || a);
a := a + 1;
IF a = 35 THEN
a := a + 1;
GOTO loopstart;
END IF;
END LOOP;
END;
/
Chapter 03 : Sequences
• A sequence is a user-def ined schema-bound object that generates a series
of numeric values.
• Sequences are frequently used in many databases because many
applications require each row in a table to contain a unique value and
sequences provide an easy way to generate them.
• The se que nce of nume ric value s is ge ne rate d in an asce nding or
descending order at def in ed intervals and can be conf igured to restart
when it exceeds max_value.
Different Features of Sequences
1. A sequence is a database object that generates and produces integer values in
sequential order.
2. It automatically generates the primary key and unique key values.
3. It may be in ascending or descending order.
4. It can be used for multiple tables.
5. Sequence numbers are stored and generated independently of tables.
6. It saves time by reducing application code.
7. It is used to generate unique integers.
8. It is used to create an auto number field.
9. Useful when you need to create a unique number to act as a primary key.
The syntax for creating a Sequence Object
is:
● Specify the name of the sequence after the CREATE SEQUENCE keywords.
If you want to create a sequence in a specific schema, you can specify the
schema name in along with the sequence name.
INCREMENT BY
● Specify the interval between sequence numbers after the INCREMENT BY keyword.
● The interval can have less than 28 digits. It also must be less than MAXVALUE -
MINVALUE.
● The default value of the first number is the minimum value of the sequence
for an ascending sequence and maximum value of the sequence for a
descending sequence.
MAXVALUE
● The min_value must be less than or equal to the first_number and must be
less than max_value.
● NOMINVALUE
● Use NOMINVALUE to indicate a minimum value of 1 for an ascending
sequence or -10^26 for a descending sequence. This is the default.
CYCLE
● Use CYCLE to allow the sequence to generate value after it reaches the
limit, min value for a descending sequence and max value for an ascending
sequence.
● Use NOCYCLE if you want the sequence to stop generating the next value
when it reaches its limit. This is the default.
CACHE
● Specify the number of sequence values that Oracle will preallocate and
keep in the memory for faster access.
Example:
create sequence A2
start with 10
increment by 10
minvalue 10
maxvalue 50
cache 2
cycle;
PL/SQL Procedure
● The PL/SQL stored procedure or simply a procedure is a PL/SQL block
which performs one or more specific tasks. It is just like procedures in other
programming languages.
● The procedure contains a header and a body.
• He ade r: The he ade r contains the name of the proce dure and the
parameters or variables passed to the procedure.
• Body: The body contains a declaration section, execution section and
exception section similar to a general PL/SQL block.
How to pass parameters in procedure:
● When you want to create a procedure or function, you have to def in e
parameters .There is three ways to pass parameters in procedure:
1. IN parameters: The IN parameter can be referenced by the procedure or
function. The value of the parameter cannot be overwritten by the
procedure or the function.
2. OUT parameters: The OUT parameter cannot be referenced by the
procedure or function, but the value of the parameter can be overwritten by
the procedure or function.
3. INOUT parameters: The INOUT parameter can be referenced by the
procedure or function and the value of the parameter can be overwritten by
the procedure or function.
Syntax: