Exp 9
Exp 9
As we want output of PL/SQL Program on screen, before Starting writing anything type (Only
Once per session)
SET SERVEROUTPUT ON
DECLARE
A INTEGER := &A;
B INTEGER := &B;
C INTEGER;
BEGIN
C := A + B;
DBMS_OUTPUT.PUT_LINE ('THE SUM IS '||C);
END;
/
Output :
Decision making with IF statement :-
IF (TEST_CONDITION) THEN
SET OF STATEMENTS
ELSIF (CONDITION)
SET OF STATEMENTS
END IF;
Ex:- Largest of three numbers.
This program can be written in number of ways, here are the two different ways to write the
program.
1) Using IF-
ELSE DECLARE
A NUMBER := &A; B
NUMBER := &B; C
NUMBER := &C; BIG
NUMBER; BEGIN
ELSE BIG
:= B;
END IF;
IF(BIG < C ) THEN DBMS_OUTPUT.PUT_LINE('BIGGEST OF A,
B AND C IS ' || C);
ELSE
DBMS_OUTPUT.PUT_LINE('BIGGEST OF A, B AND C IS ' || BIG);
END IF;
END;
/
Output :
2) Using IF—ELSIF—ELSE
DECLARE
A NUMBER := &A; B
NUMBER := &B; C
NUMBER := &C;
BEGIN
|| C);
END IF;
END;
/
Output :
Procedure in PL/SQL
Procedures are written for doing specific tasks. The general syntax of procedure is
BEGIN
PL/SQL Executable statements;
..
..
..
EXCEPTION
Exception Handlers;
END <Pro_Name>;
Mode of parameters
1) IN Mode :- IN mode is used to pass a value to Procedure/Function. Inside the
procedure/function, IN acts as a constant and any attempt to change its value causes compilation
error.
2) OUT Mode : The OUT parameter is used to return value to the calling routine. Any attempt to
refer to the value of this parameter results in null value.
3) IN OUT Mode : IN OUT parameter is used to pass a value to a subprogram and for getting the
updated value from the subprogram.
To use/call procedure, write a PL/SQL code and include call in the code using
Pro_Name(Par_List);
Or you can execute from SQL Prompt as
execute Pro_Name(Par_List)
Output :
Output :
2) Program to illustrate Procedure with IN mode
parameter. -- Assume file name P2
CREATE OR REPLACE PROCEDURE P2(A IN NUMBER) AS
BEGIN
DBMS_OUTPUT.PUT_LINE('A:'||A);
END P2;
/
Output :
DBMS_OUTPUT.PUT_LINE('X:'||X);
P2(X); DBMS_OUTPUT.PUT_LINE('X:'||
X); END;
Output :
3) Program to illustrate Procedure with OUT mode parameter.
-- Assume file name P3
CREATE OR REPLACE PROCEDURE P3(A OUT NUMBER) AS
BEGIN
A:=100;
DBMS_OUTPUT.PUT_LINE('A:'||
A); END P3;
/
Output :
DBMS_OUTPUT.PUT_LINE('X:'||X);
P3(X); DBMS_OUTPUT.PUT_LINE('X:'||
X); END;
Output :
4) Program to illustrate Procedure with OUT mode parameter.
-- Assume file name P4
CREATE OR REPLACE PROCEDURE P4(A OUT NUMBER) AS
BEGIN
DBMS_OUTPUT.PUT_LINE('A:'||A); END
P4;
Output :
DBMS_OUTPUT.PUT_LINE('X:'||X);
P4(X); DBMS_OUTPUT.PUT_LINE('X:'||
X); END;
Output :
5) Program to illustrate Procedure with IN OUT mode
parameter. --Assume file name P5
CREATE OR REPLACE PROCEDURE P5(A IN OUT NUMBER)
AS BEGIN
DBMS_OUTPUT.PUT_LINE ('A:' || A);
END P5;
/
Output :
Output :