UNIT – 1 Procedural SQL
UNIT – 1 Procedural SQL
SQL
PL/SQL Block Structure
• PL/SQL is a block structured language that can have multiple blocks in it.
• In PL/SQL all topics of PL/SQL language such as conditional statements, loops,
arrays, string, exceptions, collections, records, triggers, functions, procedures,
cursors etc.
• SQL stands for Structured Query Language i.e. used to perform operations on the
records stored in database such as inserting records, updating records, deleting
records, creating, modifying and dropping tables, views etc.
What is PL/SQL ?
• PL/SQL is a block structured language. 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. PL/SQL is integrated with
Oracle database (since version 7). The functionalities of PL/SQL usually extended
after each release of Oracle database. Although PL/SQL is closely integrated with
SQL language, yet it adds some programming constraints that are not available in
SQL.
• The Declaration section: Code block start with a declaration section, in which
memory variables, constants, cursors and other oracle objects can be declared
and if required initialized.
• The Begin section: Consist of set of SQL and PL/SQL statements, which describe
processes that have to be applied to table data. Actual data manipulation,
retrieval, looping and branching constructs are specified in this section.
• The Exception section: This section deals with handling errors that arise during
execution data manipulation statements, which make up PL/SQL code block.
Errors can arise due to syntax, logic and/or validation rule.
Radius: 9.5
Diameter: 19
Circumference: 59.69
Area: 283.53
Literals Examples
CASE [ expression ]
WHEN condition_1 THEN result_1
WHEN condition_2 THEN result_2
...
WHEN condition_n THEN result_n
ELSE result
END
Example of PL/SQL case statement
DECLARE
grade char(1) := 'A';
BEGIN
CASE grade
when 'A' then dbms_output.put_line('Excellent');
when 'B' then dbms_output.put_line('Very good');
when 'C' then dbms_output.put_line('Good');
when 'D' then dbms_output.put_line('Average');
when 'F' then dbms_output.put_line('Passed with Grace');
else dbms_output.put_line('Failed');
END CASE; Excellent
PL/SQL procedure successfully completed.
END;
PL/SQL Loop
• The PL/SQL loops are used to repeat the execution of one or more
statements for specified number of times. These are also known as
iterative control statements.
• You must follow these steps while using PL/SQL Exit Loop.
• Initialize a variable before the loop body
• Increment the variable in the loop.
• You should use EXIT WHEN statement to exit from the Loop.
Otherwise the EXIT statement without WHEN condition, the
statements in the Loop is executed only once.
Example 2
DECLARE LOOP
VAR1 NUMBER; DBMS_OUTPUT.PUT_LINE
VAR2 NUMBER; (VAR1*VAR2);
BEGIN IF (VAR2=10) THEN
VAR1:=100; EXIT;
VAR2:=1; END IF;
VAR2:=VAR2+1;
END LOOP;
END;
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.
• Syntax of while loop:
WHILE <condition>
LOOP statements;
END LOOP;
Example of PL/SQL While Loop
• DECLARE
• i INTEGER := 1;
• BEGIN
• WHILE i <= 10 LOOP
• DBMS_OUTPUT.PUT_LINE(i);
• i := i+1;
• END LOOP;
• END;
Example 2
DECLARE WHILE (VAR2<=10)
VAR1 NUMBER; LOOP
VAR2 NUMBER; DBMS_OUTPUT.PUT_LINE
BEGIN (VAR1*VAR2);
VAR1:=200; VAR2:=VAR2+1;
VAR2:=1; END LOOP;
END;
PL/SQL FOR Loop
• PL/SQL for loop is used when when you want to execute a set of statements for a
predetermined number of times. The loop is iterated between the start and end
integer values. The counter is always incremented by 1 and once the counter
reaches the value of end integer, the loop ends.
• Syntax of for loop:
FOR counter IN initial_value .. final_value LOOP
LOOP statements;
END LOOP;
PL/SQL For Loop Example 1
BEGIN
FOR k IN 1..10 LOOP
-- note that k was not declared
DBMS_OUTPUT.PUT_LINE(k);
END LOOP;
END;
Note:
• You must follow these steps while using PL/SQL FOR Loop.
• You don't need to declare the counter variable explicitly because it is
declared implicitly in the declaration section.
• The counter variable is incremented by 1 and does not need to be
incremented explicitly.
• You can use EXIT WHEN statements and EXIT statements in FOR Loops
but it is not done often.
PL/SQL For Loop Example 2
DECLARE
VAR1 NUMBER;
BEGIN
VAR1:=10;
FOR VAR2 IN 1..10
LOOP
DBMS_OUTPUT.PUT_LINE (VAR1*VAR2);
END LOOP;
END;
PL/SQL For Loop REVERSE Example
3
DECLARE
VAR1 NUMBER;
BEGIN
VAR1:=10;
FOR VAR2 IN REVERSE 1..10
LOOP
DBMS_OUTPUT.PUT_LINE (VAR1*VAR2);
END LOOP;
END;
PL/SQL Continue Statement
• The continue statement is used to exit the loop from the reminder if its body
either conditionally or unconditionally and forces the next iteration of the loop to
take place, skipping any codes in between.
• The continue statement is not a keyword in Oracle 10g. It is a new feature
encorporated in oracle 11g.
• For example: If a continue statement exits a cursor FOR LOOP prematurely then it
exits an inner loop and transfer control to the next iteration of an outer loop, the
cursor closes (in this context, CONTINUE works like GOTO).
Example of PL/SQL continue
statement
DECLARE DBMS_OUTPUT.PUT_LINE
x NUMBER := 0;
('Inside loop, after CONTINUE: x = ' ||
BEGIN
TO_CHAR(x));
LOOP -- After CONTINUE statement, control resum
es here EXIT WHEN x = 5;
DBMS_OUTPUT.PUT_LINE ('Inside loop: x = ' || T END LOOP;
O_CHAR(x));
DBMS_OUTPUT.PUT_LINE (' After loop: x
x := x + 1;
= ' || TO_CHAR(x));
IF x < 3 THEN
CONTINUE; END;
END IF; /
Out put:
• Inside loop: x = 0
• Inside loop: x = 1
• Inside loop: x = 2
• Inside loop, after CONTINUE: x = 3
• Inside loop: x = 3
• Inside loop, after CONTINUE: x = 4
• Inside loop: x = 4
• Inside loop, after CONTINUE: x = 5
• After loop: x = 5
PL/SQL - Records
• In this chapter, we will discuss Records in PL/SQL. A record is a data
structure that can hold data items of different kinds. Records consist
of different fields, similar to a row of a database table.
• For example, you want to keep track of your books in a library. You
might want to track the following attributes about each book, such as
Title, Author, Subject, Book ID. A record containing a field for each of
these items allows treating a BOOK as a logical unit and allows you to
organize and represent its information in a better way.
PL/SQL can handle the following
types of records
• Table-based
• Cursor-based records
• User-defined records
• Table-Based Records
• The %ROWTYPE attribute enables a programmer to create table-
based and cursorbased records.
• The following example illustrates the concept of table-based records. We
will be using the CUSTOMERS table we had created and used in the
previous chapters
• DECLARE
• customer_rec customers%rowtype;
• BEGIN
• SELECT * into customer_rec
• FROM customers
• WHERE id = 5;
• dbms_output.put_line('Customer ID: ' || customer_rec.id);
• dbms_output.put_line('Customer Name: ' || customer_rec.name);
• dbms_output.put_line('Customer Address: ' || customer_rec.address);
• dbms_output.put_line('Customer Salary: ' || customer_rec.salary);
• END;
• /
Out put:-
• Out put :-
• Customer ID: 5
• Customer Name: Hardik
• Customer Address: Bhopal
• Customer Salary: 9000
• Title
• Author
• Subject
• Book ID
Define a record type :-
• TYPE
• type_name IS RECORD
• ( field_name1 datatype1 [NOT NULL] [:= DEFAULT EXPRESSION],
• field_name2 datatype2 [NOT NULL] [:= DEFAULT EXPRESSION],
• ...
• field_nameN datatypeN [NOT NULL] [:= DEFAULT EXPRESSION);
• record-name type_name;
The Book record is declared in the
following way
DECLARE
TYPE books IS RECORD
(title varchar(50),
author varchar(50),
subject varchar(100),
book_id number);
book1 books;
book2 books;
Accessing Fields
• To access any field of a record, we use the dot (.) operator. The member access operator is coded
as a period between the record variable name and the field that we wish to access. Following is
an example to explain the usage of record −
• Program:
DECLARE
type books is record
(title varchar(50),
author varchar(50),
subject varchar(100),
book_id number);
book1 books;
book2 books;
BEGIN
-- Book 1 specification
book1.title := 'C Programming';
book1.author := 'Nuha Ali';
book1.subject := 'C Programming Tutorial';
book1.book_id := 6495407;
-- Book 2 specification
book2.title := 'Telecom Billing';
book2.author := 'Zara Ali';
book2.subject := 'Telecom Billing Tutorial';
book2.book_id := 6495700;
-- Print book 1 record
dbms_output.put_line('Book 1 title : '|| book1.title);
dbms_output.put_line('Book 1 author : '|| book1.author);
dbms_output.put_line('Book 1 subject : '|| book1.subject);
dbms_output.put_line('Book 1 book_id : ' || book1.book_id);