Control Statement - in - PLSQL

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 31

Procedural Language Extension to SQL

 Allows using general programming tools with


SQL, for example: loops, conditions,
functions, etc.
 This allows a lot more freedom than general
SQL, and is lighter-weight than JDBC.
 We write PL/SQL code in a regular file, for
example PL.sql, and load it with @PL in the
sqlplus console.
Compared to SQL, PL/SQL has the procedural constructs that are useful
.to express a desired process from start to end

One block of PL/SQL code can bundled several SQL statements


together as a single unit. Making less network traffic and improving
.application performance

PL/SQL can be integrated with other languages, such as Java, to take


.advantage of the strongest features of both languages
 PL/SQL code is built of Blocks, with a unique
structure.
 There are two types of blocks in PL/SQL:
1. Anonymous Blocks: have no name (like scripts)
 can be written and executed immediately in SQLPLUS
 can be used in a trigger
2. Named Blocks:
 Procedures
 Functions
Anonymous Procedure Function

>PROCEDURE <name >FUNCTION <name


IS >RETURN <datatype
DECLARE
BEGIN IS
BEGIN statements- BEGIN
-statements EXCEPTION statements-
EXCEPTION ;END EXCEPTION
END; ;END
DECLARE (optional)
/* Here you declare the variables you will use in
this block */
BEGIN (mandatory)
/* Here you define the executable statements (what
the block DOES!)*/
EXCEPTION (optional)
/* Here you define the actions that take place if an
exception is thrown during the run of this block
*/
END; (mandatory)
/ A correct completion of a block
will generate the following
Always put a new line with only :message
a / at the end of a block! (This
PL/SQL procedure successfully
tells Oracle to run the block)
completed
Syntax
identifier
identifier [CONSTANT]
[CONSTANT] datatype
datatype [NOT
[NOT NULL]
NULL]

[:=
[:= || DEFAULT
DEFAULT expr];
expr];
Notice that PL/SQL
Examples includes all SQL types,
…and more

Declare
Declare
birthday
birthday DATE;
DATE;
age
age NUMBER(2)
NUMBER(2) NOT
NOT NULL
NULL :=
:= 27;
27;
name
name VARCHAR2(13)
VARCHAR2(13) :=:= 'Levi';
'Levi';
magic
magic CONSTANT
CONSTANT NUMBER
NUMBER :=:= 77;
77;
valid
valid BOOLEAN
BOOLEAN NOT
NOT NULL
NULL :=
:= TRUE;
TRUE;
DECLARE message varchar2(20):= 'Hello, World!';
BEGIN
dbms_output.put_line(message);
END;
SQL>/
 DECLARE
 a integer := 10;
 b integer := 20;
 c integer; f real;
 BEGIN
 c := a + b;
 dbms_output.put_line('Value of c: ' || c);
 f := 70.0/3.0;
 dbms_output.put_line('Value of f: ' || f);
 END;
 /
Conditional IF <boolean expression> THEN
Processing <sequence of statements>
END IF;
The specified --------------------------------------------------------------------------------
-------------

conditions are
evaluated by the IF <boolean expression> THEN
system and the <sequence of statements>
result ELSE
determines which <sequence of statements>
sequence of END IF;
statements is to be
carried out.
IF <boolean expression> THEN
>sequence of statements<
ELSIF <boolean expression> THEN
>sequence of statements<
ELSIF <boolean expression> THEN
>sequence of statements<
ELSIF <boolean expression> THEN
>sequence of statements<
ELSE
>sequence of statements<
;END IF

Dr. James Dullea, CSC8490 Introduction to


PL/SQL
Slide 13 of 36
 Unconstrained loops
 WHILE loop
 FOR loop
 GOTO <<LABEL>>
LOOP
<sequence of
statements>
EXIT WHEN <condition>
<sequence of
statements>
END LOOP;
WHILE <condition>
LOOP
<statements>
END LOOP;
Note: The loop will continue to process as
long as the condition is TRUE or an EXIT (or
EXIT WHEN) statement is encountered.
FOR <loop_counter> IN [REVERSE]
<low bound>..<high bound>
LOOP
<sequence of statements>
END LOOP;

Dr. James Dullea, CSC8490 Slide


17 of
Introduction to PL/SQL 36
GOTO label;
The label is defined in the block by
being enclosed in double angle brackets.
Example:
LOOP
<sequence of statements>
IF <condition> THEN
GOTO get_out_of_loop;
<sequence of statements>
END LOOP;
<<get_out_of_loop>>

Dr. James Dullea, CSC8490 Slide


18 of
Introduction to PL/SQL 36
:Condition :Nested conditions
If <cond> If <cond>
then <command>
then
elsif <cond2>
then <command2> if <cond2>
else then
<command3> <command1>
end if;
end if;
else <command2>
end if;
. . .
IF rating > 7 THEN
v_message := 'You are great';
ELSIF rating >= 5 THEN
v_message := 'Not bad';
ELSE
v_message := 'Pretty bad';
END IF;
. . .
declare
a number:='&a';
begin
if mod(a,2)=0
then
dbms_output.put_line('even number');
else
dbms_output.put_line('odd number');
end if;
end;
 SET SERVEROUTPUT ON to allow output to
be displayed to the screen
 DBMS_OUTPUT.PUT_LINE
◦ Usage:
DBMS_OUTPUT.PUT_LINE ( Argument )
◦ Argument tendS to resemble the concatenated
arguments of the SELECT clause in an SQL
query.
◦ If the argument is not initialized, then a NULL
VALUE will be displayed.
create table number_table(
num NUMBER(10)
);

DECLARE
i number_table.num%TYPE := 1;
BEGIN
LOOP
INSERT INTO number_table
VALUES(i);
i := i + 1;
EXIT WHEN i > 10;
END LOOP;
END;
create table number_table(
num NUMBER(10)
);

DECLARE
cursor c is select * from number_table;
cVal c%ROWTYPE;
BEGIN
open c;
LOOP
fetch c into cVal;
EXIT WHEN c%NOTFOUND;
insert into doubles values(cVal.num*2);
END LOOP;
END;
DECLARE
i number_table.num%TYPE;
BEGIN
FOR i IN 1..10 LOOP
INSERT INTO number_table VALUES(i);
END LOOP;
END;

Notice that i is incremented


automatically
DECLARE
TEN number:=10;
i number_table.num%TYPE:=1;
BEGIN
WHILE i <= TEN LOOP
INSERT INTO number_table
VALUES(i);
i := i + 1;
END LOOP;
END;
 You need to use a function in the
DBMS_OUTPUT package in order to print to
the output
 If you want to see the output on the screen,
you must type the following (before starting):
set serveroutput on format wrapped size 1000000
 Then print using
◦ dbms_output. put_line(your_string);
◦ dbms_output.put(your_string);
set serveroutput on format wrap size 1000000
ACCEPT high PROMPT 'Enter a number: '

DECLARE
i number_table.num%TYPE:=1;
BEGIN
dbms_output.put_line('Look Ma, I can print from PL/SQL!!!');
WHILE i <= &high LOOP
INSERT INTO number_table
VALUES(i);
i := i + 1;
END LOOP;
END;
DECLARE (optional)
/* Here you declare the variables you will use in
this block */
BEGIN (mandatory)
/* Here you define the executable statements (what
the block DOES!)*/
EXCEPTION (optional)
/* Here you define the actions that take place if an
exception is thrown during the run of this block
*/
END; (mandatory)
/
Set serveroutput on
Accept p_price Prompt 'Enter the Price: '
DECLARE
v_inv_value number(8,2);
v_price number(8,2);
v_quantity number(8,0) := 400;
BEGIN
v_price := &p_price;
v_inv_value := v_price * v_quantity;
dbms_output.put_line('******');
dbms_output.put_line('price * quantity=');
dbms_output.put_line(v_inv_value);
END;
/
Note: PL/SQL not designed for user interface programming
PL/SQL . Write the PL/SQL program
1. Find the area of the triangle.
2. Find the area of the circle
3. To print all even numbers below 100.
4. To reverse the given number.
5. To find the factorial of a given number.
6. To generate the Fibonacci series up to ‘n’
terms
7. To check whether the given number is
Armstrong number or not.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy