Professional Documents
Culture Documents
UNIT 4
What is PL/SQL?
BEGIN
emp_name:=‘abc’;
birthdate:=‘19-Dec-1983’;
valid_id:= FALSE;
bonus := salary * 0.10;
CONSTANT
In constant declarations, the reserved word
CONSTANT must precede the type specifier.
DECLARE
credit_limit CONSTANT number := 5000.00;
%TYPE
DECLARE
name EMPLOYEE.ename %TYPE;
1. IF THEN Statement
4. Nested IF
IF THEN Statement
Syntax:
IF condition THEN
Statement1;
END IF;
Using the IF-THEN Statement
The simplest form of IF statement associates a
condition with a sequence of statements enclosed by
the keywords THEN and END IF (not ENDIF)
IF condition THEN
Statement1;
ELSE
Statement 2;
END IF;
Using the IF-THEN-ELSE
Statement
The second form of IF statement adds the
keyword ELSE followed by an alternative
sequence of statements.
DECLARE
start_no number(3);
counter number(2);
BEGIN
counter:=0;
start_no:=&start_no;
LOOP
counter:=counter+1;
DBMS.OUTPUT.PUT.LINE(‘Number : start_no);
Start_no = start_no+1;
EXIT WHEN counter >10;
END LOOP;
END;
/
Simple Loop
These are the important steps to be followed while
using Simple Loop.
Initialize a variable before the loop body.
Increment the variable in the loop.
Use a EXIT WHEN statement to exit from
the Loop.
If you use a EXIT statement without WHEN
condition, the statements in the loop is
executed only once.
Using the EXIT Statement
When an EXIT statement is encountered, the loop completes immediately and control
passes to the statement immediately after END LOOP
Example
DECLARE
x NUMBER := 0;
BEGIN
LOOP
DBMS_OUTPUT.PUT_LINE ('Inside loop: x = ' || x);
x := x + 1;
IF x > 3 THEN
EXIT;
END IF;
END LOOP;
-- After EXIT, control resumes here
DBMS_OUTPUT.PUT_LINE (' After loop: x = ' || x);
END;
/
OUTPUT
Inside loop: x = 0
Inside loop: x = 1
Inside loop: x = 2
Inside loop: x = 3
After loop: x = 4
What is the O/P?
Example
DECLARE
x NUMBER := 0;
BEGIN
LOOP
DBMS_OUTPUT.PUT_LINE ('Inside loop: x = ' || x);
x := x + 1;
EXIT;
END LOOP;
-- After EXIT, control resumes here
DBMS_OUTPUT.PUT_LINE (' After loop: x = ' || x);
END;
/
While LOOP
A WHILE LOOP is used when a set of statements
has to be executed as long as a condition is true.
The condition is evaluated at the beginning of each
iteration. The iteration continues until the condition
becomes false.
The General Syntax to write a WHILE LOOP is:
WHILE <condition>
LOOP
Statements;
END LOOP;
For LOOP
A FOR LOOP is used to execute a set of statements for a
predetermined number of times.
Iteration occurs between the start and end integer values given.
The counter is normally incremented by 1. The loop exits when
the counter reaches the value of the end integer.
Syntax
FOR variable IN [REVERSE] start..end
LOOP
Statements;
END LOOP;
For LOOP
• Variable – is used as loop index. This variable
need not be declared and is always
incremented by 1.
• Start is an integer value. Represents the
beginning of loop index
• End is an integer value. Represents the last
value of loop index
• REVERSE starts the loop in reverse order,
starting from End to Start.
Write a PL/SQL block to print 10 numbers
starting from 1 using FOR loop.
BEGIN
FOR i IN 1..10
LOOP
DBMS_OUTPUT.PUT_LINE(i);
END LOOP;
END;
Write PL/SQL block to print 10 sequential numbers in
descending order starting from 10.
BEGIN
FOR i IN REVERSE 1..10
LOOP
DBMS_OUTPUT.PUT_LINE(i);
END LOOP;
END;
Write a PL/SQL block to check whether the given
string is a palindrome or not
DECLARE
IF str1=str2 THEN
str1 varchar2(20);
str2 varchar2(10); DBMS_OUTPUT.PUT_LINE(
chr1 char(1); str1|| ‘is palindrome’);
len number(2);
BEGIN ELSE
str1:=&str1;
DBMS_OUTPUT.PUT_LINE(
len:=LENGTH(str1);
str1|| ‘is not a palindrome’);
FOR i in reverse 1..len
LOOP
chr1:=SUBSTR(str1,i,1);
str2:=str2 || chr1;
END LOOP;
Sequential Control(Unstructured Batch)
DECLARE <<even_no>>
num1 number(3);
rem number(3); DBMS_OUTPUT.PUT_LINE
(‘number is
BEGIN even’);
num1:=&num1; <<odd_no>>
rem:=mode(num1,2);
DBMS_OUTPUT.PUT_LINE
IF rem=0 THEN (‘number is
goto even_no; odd’);
ELSE END;
goto odd_no;
END IF;