Features of PL/SQL
Features of PL/SQL
BASIC SYNTAX
PL/SQL is a block-structured language, meaning that PL/SQL programs are divided and
written in logical blocks of code. Each block consists of three sub-parts:
Every PL/SQL statement ends with a semicolon (;). PL/SQL blocks can be nested within
other PL/SQL blocks using BEGIN and END. The basic structure of a PL/SQL block is as
follows:
DECLARE
<declarations section>
BEGIN
<executable command(s)>
EXCEPTION
<exception handling>
END;
The end; line signals the end of the PL/SQL block. To run the code from SQL command line,
you may need to type / at the beginning of the first blank line after the last line of the code.
The PL/SQL Identifiers
PL/SQL identifiers are constants, variables, exceptions, procedures, cursors, and reserved
words. The identifiers consist of a letter optionally followed by more letters, numerals, dollar
signs, underscores, and number signs and should not exceed 30 characters. By default,
identifiers are not case-sensitive. So you can use integer or INTEGER to represent a numeric
value. You cannot use a reserved keyword as an identifier.
How can PL/SQL be used in SQL*Plus?
Ans-PL/SQL doesn’t support interactive I/O, so it needs a tool like SQL*Plus to:
(i) Input, store and run PL/SQL programs
(ii) Create load and run scripts containing PL/SQL blocks.
(iii) Call a stored procedure.
Q. What are scalar data types? Give examples.
Ans. A scalar data-type is one, which is not made up of other data types. In other
words, a scalar data type doesn't have sub-components i.e., is atomic in nature. Examples of
scalar data types are : CHAR, VARCHAR2, NUMBER, DATE etc.
Q. What are composite data types? Give examples.
Ans. Data-types that are made up of other data types are called composite data types.
These data types contain sub-components, (hat can be accessed and manipulated individually
are called composite data-types e.g., RECORD and TABLE (as supported by PL/SQL); and
VARRAY.
Q. How many types of variables does PL/ SQL support?
Ans. PL/SQL supports three types of variables, namely: Local variables. Substitution
variables and Bind variables
Q. What are comments? What are the different ways of adding comments in PL/SQL?
Ans. Comments are statements that are ignored by the compiler and axe used to enhance
program readability. PL/SQL supports two ways of adding comments:
> Single line comments. These comments start with the sign '--'
> Multi-line comments. These comments start with /* and end with •/.
Q. What does the statement SET SERVEROUTPUT ON mean ?
Ans. SERVEROUTPUT is an environment variable with two possible values ON or OFF.
The output generated by the DBMS..OUTPUT package is visible only - when
SERVEROUTPUT variable's set to ON.
Q. What are literals ?
Ans. Literals are the values that have not been associated with an identifier. 10, 'test', '12-
JAN-
2003' are all examples of literals of different data-types.
According to the structure theorem, any computer program can be written using the basic
control structures shown in the following figure. They can be combined in any way necessary
to deal with a given problem.
The selection structure tests a condition, then executes one sequence of statements instead of
another, depending on whether the condition is true or false.
A condition is any variable or expression that returns a Boolean value (TRUE or FALSE).
The iteration structure executes a sequence of statements repeatedly as long as a condition
holds true. The sequence structure simply executes a sequence of statements in the order in
which they occur.
IF-THEN Statement
IF condition THEN
sequence_of_statements
END IF;
The sequence of statements is executed only if the condition is true. If the condition is false
or null, the IF statement does nothing. In either case, control passes to the next statement. An
example follows:
IF-THEN-ELSE Statement
The second form of IF statement adds the keyword ELSE followed by an alternative
sequence of statements, as follows:
IF condition THEN
sequence_of_statements1
ELSE
sequence_of_statements2
END IF;
IF-THEN-ELSIF Statement
Sometimes you want to select an action from several mutually exclusive alternatives. The
third form of IF statement uses the keyword ELSIF (not ELSEIF) to introduce additional
conditions, as follows:
IF condition1 THEN
sequence_of_statements1
ELSIF condition2 THEN
sequence_of_statements2
ELSE
sequence_of_statements3
END IF;
CASE Statement
Like the IF statement, the CASE statement selects one sequence of statements to execute.
However, to select the sequence, the CASE statement uses a selector rather than multiple
Boolean expressions. A selector is an expression whose value is used to select one of several
alternatives. To compare the IF and CASE statements, consider the following code that
outputs descriptions of school grades:
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('Fair');
WHEN 'F' THEN dbms_output.put_line('Poor');
ELSE dbms_output.put_line('No such grade');
END CASE;
LOOP statements let you execute a sequence of statements multiple times. There are three
forms of LOOP statements: LOOP, WHILE-LOOP, and FOR-LOOP.
LOOP
The simplest form of LOOP statement is the basic (or infinite) loop, which encloses a
sequence of statements between the keywords LOOP and END LOOP, as follows:
LOOP
sequence_of_statements
END LOOP;
EXIT
The EXIT statement forces a loop to complete unconditionally. When an EXIT statement is
encountered, the loop completes immediately and control passes to the next statement. An
example follows:
LOOP
...
IF credit_rating < 3 THEN
...
EXIT; -- exit loop immediately
END IF;
END LOOP;
EXIT-WHEN
The EXIT-WHEN statement lets a loop complete conditionally. When the EXIT statement is
encountered, the condition in the WHEN clause is evaluated. If the condition is true, the loop
completes and control passes to the next statement after the loop. An example follows:
LOOP
FETCH c1 INTO ...
EXIT WHEN c1%NOTFOUND; -- exit loop if condition is true
...
END LOOP;
CLOSE c1;
WHILE-LOOP
FOR-LOOP
Whereas the number of iterations through a WHILE loop is unknown until the loop
completes, the number of iterations through a FOR loop is known before the loop is entered.
FOR loops iterate over a specified range of integers. The range is part of an iteration scheme,
which is enclosed by the keywords FOR and LOOP. A double dot (..) serves as the range
operator. The syntax follows:
Dynamic Ranges
PL/SQL lets you determine the loop range dynamically at run time, as the following example
shows:
Q4. WAP in PL/SQL to inverse a number, eg. Number 5639 when inverted must be
display
output 9365.