Control Statement - in - PLSQL
Control Statement - in - PLSQL
Control Statement - in - PLSQL
[:=
[:= || 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
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;
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.