You are on page 1of 4

VARIABLE

In Oracle DBMS there are two types of variables


1 User variables
2 Bind variables
1 User variables: This is just like normal variables who can hold a data in computer memory
and this type of variables are only can declare inside the declare block.

Declare
Var number(10);
Begin
DBMS_OUTPUT.PUTLINE(Var);
END;

2 Bind variables: This is also like normal variables but it can be declare in anywhere in the host
environment means we can declare the variable outside the declare block.

VARIABLE Var number(10);


print :Var;

CONSTANT
Constant: Constant are also place holders like variables except once the value is assigned can’t
be changed during the program execution time.

Flow Control Statements


In oracle sql we also can use conditional statements and iterative statements to change the flow
of the program.

Conditional Statements
In oracle sql we have three types of Conditional Statements. They are
1 IF-THEN
2 IF-THEN-ELSE
3 IF-THEN-ELSIF-ELSE

1 IF-THEN:

DECLARE
X NUMBER(10);
BEGIN
X:=:X;

IF X<10 THEN
DBMS_OUTPUT.PUT_LINE(' X < 10 ');
END IF;
END;
2 IF-THEN-ELSE:

DECLARE
X NUMBER(10);
BEGIN
X:=:X;

IF X MOD 2 = 0 THEN
DBMS_OUTPUT.PUT_LINE(X||' is prime');
ELSE
DBMS_OUTPUT.PUT_LINE(X||' is not prime');
END IF;
END;
3 IF-THEN-ELSIF-ELSE:

DECLARE
X NUMBER(4);
BEGIN
X:=:X;

IF X>80 THEN
DBMS_OUTPUT.PUT_LINE('O');
ELSIF X<=80 AND X>60 THEN
DBMS_OUTPUT.PUT_LINE('E');
ELSIF X<=60 AND X>40 THEN
DBMS_OUTPUT.PUT_LINE('A');
ELSE
DBMS_OUTPUT.PUT_LINE('F');
END IF;

END;

Iterative Statements
In oracle sql we have three types of loops. They are
1 For Loop
2 While Loop
3 Simple Loop
1 For Loop: In oracle sql we use for loop like this

DECLARE
X NUMBER(10);
BEGIN
X:=:X;

FOR I IN 1..X LOOP


DBMS_OUTPUT.PUT_LINE(I);
END LOOP;

END;
In oracle if we want to use for loop in descending order then we have to use REVERSE key
word like this.
DECLARE
X NUMBER(10):=10;
BEGIN
FOR I IN REVERSE 0..X LOOP
DBMS_OUTPUT.PUT_LINE(I);
END LOOP;
END;

2 While Loop: In oracle sql we use while loop like this

DECLARE
X NUMBER(10);
I NUMBER(10);
BEGIN
X:=:X;
I := 0;

WHILE I<X LOOP


DBMS_OUTPUT.PUT_LINE(I);
I := I+1;
END LOOP;
END;
3 Simple Loop: In oracle sql we have a special loop called Simple Loop which works like this

DECLARE
X NUMBER(10):=10;
I NUMBER(10):=0;
BEGIN
LOOP
DBMS_OUTPUT.PUT_LINE(I);
I:=I+1;
IF I>10 THEN
EXIT;
END IF;

END LOOP;
END;
Triggers
In oracle pl/sql we have a concept called Triggers. Actually triggers are nothing but a block of
named block which execute when any event occur like DDL,DML etc…

You might also like